I would like to check whether a certain thread has already been joined.
In the code below I have threads that finish at different times and I would like to check whether a thread has terminated and not yet been joined.
Is there a good way of checking this?
while(!allJoined){
allJoined=true;
for ( int i = 0; i < 10; i++ )
{
try {
if(!threadList[i].isAlive() && threadList[i].NOT_YET_JOINED() ) {
threadList[i].join(0);
System.out.println("Joined t-"+i);
} else {
allJoined = false;
}
} catch (Exception e) {
System.out.println("MASTER: Child interrupted."+e);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
}
Whether you have
join()ed a thread is a part of your state, not the thread’s state. Imagine: you could have several threads all trying to join a given worker-thread.Why don’t you keep a list of the threads you have already joined?
Then check that list before you
joinit.then use joined.size() to see if all are joined or not.