I was wondering how Java implements the join() method to wait for a thread to finish. According to the source code:
public final synchronized void [More ...] 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;
}
}
}
The calling thread obtains the running thread's monitor and wait() at line 1160 indefinitely while running thread is still alive.
My question is: where is (and who calls) the notify() or notifyAll() when the thread completes, so that it wakes up the calling thread?
To be perfectly clear, the question is about where in the JDK/JVM is notify() called, not in our code.
Thank you.
notifyAll (or its native equivalent) is called in ensure_join in src/share/vm/runtime/thread.cpp, at line 1526 in the current version, which is called from JavaThread::exit in the same file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With