new to multithreading- how to use wait() and notify() in java?

时间秒杀一切 提交于 2020-01-13 05:33:33

问题


I'm trying to write a program with 2 classes, a controller and a class that does a lot of computation. The controller creates a couple instances of the other class, then tells them all to start their calculations (in parallel). They each return when they are finished, and the controller resumes, and then, some time later, the controller passes them new data, and has them run the calculation again.
Ideally, I would be able to call start() with parameters, but that isn't possible, so the controller calls a method in the calculator that stores the data in a global, and then starts the calculation thread and returns, which works until I try to start the thread again, and it tells me the thread is dead. So I'm trying to make the run an infinite loop that just waits until it is notified, runs the calculations, stores the results in a global so the controller can retrieve them later, then resumes waiting. So something like:

//in controller:
Calculator calc=new Calculator();
calc.runCalculations(data);
while(calc.isCalculating()){
    //do nothing
}
System.out.println("results: "+calc.getAnswer());
calc.runCalculations(data2);

//in calculator:
public runCalculations(Data data){
    globalData=data;
    this.notify();
}
public void run(){
    while(true){
        synchronized(this){
            wait();
        }
        calculating=true;
        globalData=calculate(globalData);
        calculating=false;
    }
}

回答1:


You need to obtain the monitor on this before you call notify(). Also, when you call wait() you should do so in a loop that checks a condition to be certain that you did not experience a spurious wakeup.

public runCalculations(Data data){
    globalData=data;
    synchronized(this) {
        calculating=true;
        this.notify();
    }
}
public void run(){
    while(true){
        synchronized(this){
            calculating=false;
            while(!calculating) {
                wait();
            }
        }
        globalData=calculate(globalData);
    }
}

In general, most people would advise against using wait/notify and instead recommend you use an Executor or at the very least a BlockingQueue. But both of those would require you to redesign what you are currently thinking.



来源:https://stackoverflow.com/questions/3278234/new-to-multithreading-how-to-use-wait-and-notify-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!