0

Room 下面是允许线程进入和退出房间的类。Orchestrate_enque_deque 是帮助线程进入然后执行同步变量的增量并等待所有线程完成的类。

我应该做什么:

最后一个要完成的线程将调用退出处理程序。然后退出处理程序将再次通知所有线程进入下一个房间并再次继续此操作直到最后一个房间。

我的问题 :

我怎么知道最后一个线程是什么时候执行的?进入房间 0 时,会创建 13 个线程。所有 13 个线程 increment() 共享变量。线程必须等到所有线程完成计算。现在我怎么知道所有线程都完成了计算。

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();
    }

}


}
4

1 回答 1

0

我会查看 java.util.concurrent 中提供的 CompletionService Api

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CompletionService.html

于 2013-11-04T19:20:13.617 回答