How is thread.join() implemented
Hi guys. The question is as in title. How is thread,join() implemented? Does it sleeps for a while and checks if the thread is still active or its a lot lower level than this?
What I want to do is have a threadgroup, say group, and do
if (group.activeCount() > 0){
sleep(some amount);
}
and I want to see whether this is more inefficient than doing:
for (int it = 0; i < MAX_THREADS; i++){
thread[i].join;
}
thx
Message was edited by:
JohnFish
[730 byte] By [
JohnFisha] at [2007-10-2 21:08:54]

If you want to wait for threads to exit, use join. That's what it's for. I don't think activecount is even guaranteed to be accurate.
If you want to know how it's implemented, you can look in the source. src.zip is included with the JDK download. Here's the 5.0 implementation:
<pre>
/**
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @parammillisthe time to wait in milliseconds.
* @exception InterruptedException if another thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
</pre>
jverda at 2007-7-13 23:54:48 >
