Room下面是允许线程进出房间的类。 Orchestrate_enque_deque是帮助线程进入然后执行synchronized变量增量的类,并等待所有线程完成。
我应该做的事情:
完成的最后一个线程将调用退出处理程序。然后退出处理程序将再次通知所有线程进入下一个房间并再次继续这个直到最后一个房间。
我的问题:
我如何知道最后一个线程何时执行? 输入0时,创建13个线程。所有13个线程增量()共享变量。线程必须等到所有线程都完成计算。现在我怎么知道所有线程都已完成计算。
Orchestrate_enque_deque
的功能内部 increase();
System.out.println(count);
while(roomobj.enter_room){
cond.await();
System.out.println("now before signal all");
}
每个线程都会增加并等待,并且永远不会发出唤醒信号。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Rooms{
int room;
boolean enter_room = true;
int which_room=0;
public interface Handler{
void onEmpty() throws InterruptedException;
}
public Rooms(int m){
this.room = m;
}
public void enter(int i){
System.out.println("room.enter "+ i );
if(which_room == i) {
enter_room = true;
}
}
public boolean exit(){
if(room < which_room)
return true;
else
{
return false;
}
}
public void setExitHandler(int i, Rooms.Handler h) throws InterruptedException {
h.onEmpty();
}
}
class Orchestrate_enque_deque implements Rooms.Handler{
int count = 0;
final Lock lock = new ReentrantLock();
final Condition cond = lock.newCondition();
Rooms roomobj;
int which_room = 0;
int room_no;
Rooms.Handler handler;
Orchestrate_enque_deque(int room_no){
this.room_no = room_no;
roomobj = new Rooms(room_no);
}
public boolean when_to_exit(){
return roomobj.exit();
}
public void increase(){
for(int i =0;i <100;i++){
count++;
}
}
public void work() throws InterruptedException{
lock.lock();
//while()
// wait till all thread finish job in room.
while(roomobj.enter_room == false)
cond.await();
roomobj.enter(which_room);
try{
if(which_room >0 && which_room < room_no){
System.out.println("orchestrate work : which_room" + which_room );
cond.signalAll();
}
increase();
System.out.println(count);
while(roomobj.enter_room){
cond.await();
System.out.println("now before signal all");
}
//roomobj.setExitHandler(i, h);
}finally{
// roomobj.setExitHandler(i, handler);
lock.unlock();
}
}
public void onEmpty() throws InterruptedException {
roomobj.enter_room = false;
which_room++;
System.out.println("inside onEmpty : which_room after adding" + which_room);
}
}
class Worker extends Thread{
Orchestrate_enque_deque obj;
public Worker(Orchestrate_enque_deque obj){
this.obj = obj;
}
public void run(){
try {
while(!obj.when_to_exit()){
obj.work();
//System.out.println(" I am thread doing run with id "+Thread.currentThread().getId() );
obj.onEmpty();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Simulate{
public static void main(String args[]) throws InterruptedException {
int NO_OF_THREADS = 13;
Orchestrate_enque_deque Orchestrate_obj = new Orchestrate_enque_deque(4);
Worker[] worker_obj = new Worker[NO_OF_THREADS];
for(int i = 0; i < NO_OF_THREADS;i++){
worker_obj[i] = new Worker(Orchestrate_obj);
worker_obj[i].start();
}
for(int j = 0; j < NO_OF_THREADS;j++){
worker_obj[j].join();
}
}
}
答案 0 :(得分:0)
我将查看java.util.concurrent
中提供的CompletionService Apihttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CompletionService.html