I know there are more posts about this. But I can't really wrap my head around the answers. Both the summer heat and the subject is making it hard. So I was hoping that someone can help me with my specific case.
What I want is the following:
static public <T> void update(Quad_Tree<T> qt) {
Thread[] threads = new Thread[4];
for (int i = 0; i < 4; i++) {
final int index = i;
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
update(qt.root.children[index]);
}
});
threads[index] = thread;
thread.start();
}
// What is a good way to know those threads are done?
// ...
}
static public <T> void update(Quad_Tree_Node<T> qtn) {
// my update function that does not need synchronized and is totally thread safe
}
I prefer a solution that does not introduce tons of java classes. And where I need to do a lot of digging to know what they are doing in the background.
I tried this, but it does not work:
while (true) {
int count = 0;
if (!threads[0].isAlive()) count++;
if (!threads[1].isAlive()) count++;
if (!threads[2].isAlive()) count++;
if (!threads[3].isAlive()) count++;
if (count == 4) {
break;
}
}