I have a class that spawns a bunch of threads and needs to wait until all the generated threads complete. (I need to calculate the completion time of all threads).
MainClass creates all the threads and then checks to see if all the threads are complete before it can call itself complete.
Will this logic work. If so, is there a better way to do this? If not, I would like to better understand this scenario.
class MainClass{
private boolean isCompleted;
...
for(task : tasks){
threadpool.execute(task);
}
for(task : tasks){
if(!task.isCompleted()){
task.wait()
}
}
isCompleted = true;
}
class Task{
public void run(){
....
....
synchronized(this){
task.completed = true;
notifyAll();
}
}
}
source
share