Lets say I have a large number of worker threads all actively processing, and a supervisor thread that waits for them all to complete. Traditionally I could do something like:
for(Worker w:staff){
w.start();
}
for(Worker w:staff){
w.join();
}
..and all would be well. However in this case the size of my worker list (ArrayList staff) is variable and may change at arbitrary times. My best knowledge tells me something like this should work:
synchronized(this){
for(Worker w:staff){
w.start();
}
}
while(true){ //this loop will keep the supervisor running until all workers have stopped
//interrupts will occur when changes to the size of staff list occur
try{
Iterator<Worker> it;
synchronized(this){
it = staff.iterator();
}
while(it.hasNext()){
Worker w = it.next();
w.join();
}
return;
}catch(InterruptedException ie){continue;}
}
This however, still results in a conncurrentModificationException on the line
Worker w = it.next();
...which to me seems strange, because I thought that once you retrieved the iterator it was separate from the list itself. My next idea is to clone the iterator or staff list so that it IS separate from the original changing list, but I thought I would give you experts a shot at it first.