Join of java

call join, in the main function to view the source code of join. Calling join will be converted to calling wait (0), such as the following code

public static void main(String[] args) {
    Thread r1 = new Thread(new X("r1"))
    r1.start();
    try {
        r1.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Main finished");
}

however, generally speaking, when A calls wait, you need to use notify or notifyAll to wake up thread A, but you do not see these two functions on the notify, of the main thread in the join code, so how does the main come out of the wait (0) in join and then loop ? (the following is the source code of join. Calling join (), will call join (0) directly inside join (), that is, the following function)

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;
        }
    }
}
Jun.05,2021

Thread automatically implements the notifyAll function after execution, which belongs to the jvm cPP level, so there is no need to display the notify

in the java code.
Menu