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