0

Simply put, How does the join() method of Thread works internally, I know that using join() method you can join one thread at the end of other.

For example, if we use thread.join() in main thread, then the main thread should wait until run method of Thread thread finishes completely. But how it happens internally, will this append the stack of main thread at the end of thread's stack. I am confused as two threads cannot share the same stack. Please correct me if I am wrong.

webspider
  • 67
  • 1
  • 6

2 Answers2

1

Here's the implementation from the Oracle JDK (as seen in jdk1.8.0_05/src.zip)

/**
 * Waits at most {@code millis} milliseconds for this thread to
 * die. A timeout of {@code 0} means to wait forever.
 *
 * <p> This implementation uses a loop of {@code this.wait} calls
 * conditioned on {@code this.isAlive}. As a thread terminates the
 * {@code this.notifyAll} method is invoked. It is recommended that
 * applications not use {@code wait}, {@code notify}, or
 * {@code notifyAll} on {@code Thread} instances.
 *
 * @param  millis
 *         the time to wait in milliseconds
 *
 * @throws  IllegalArgumentException
 *          if the value of {@code millis} is negative
 *
 * @throws  InterruptedException
 *          if any 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;
        }
    }
}

As you can see, no fancy stack manipulation occurs. They simply use the wait and notify methods to have one thread wait until the other notifies it of its termination.

meriton
  • 68,356
  • 14
  • 108
  • 175
  • This is what I was looking for, but the thing is how the waiting thread is notified. There is no notify in the join method – webspider May 30 '14 at 15:44
  • @user2820072 there is no notify in the join() method because the join() method is the one that's waiting. It's the thread that you're waiting _for_ that calls notify(). The thread does not immediately die when the run() method returns. The run() method was _called_ by something (e.g., by a private method of the Thread class, or by a JNI method), and that caller is what clears the isAlive() flag and then notifies the Thread object. Only then does the thread _truly_ die. – Solomon Slow May 30 '14 at 17:58
0

Each JVM might implement this differently, but I think most (or at least Oracle and openJDK) do end up calling pthread_join.

And how does pthread_join works? There's already a question with that: How is pthread_join implemented?

I don't think anthing would fiddle with the stack in the way you suggest. As the I linked suggest, it's probably waiting on a semaphore.

Community
  • 1
  • 1
Augusto
  • 28,839
  • 5
  • 58
  • 88