Join method for the Java basic tutorial

  • 2020-04-01 02:43:44
  • OfStack

The contents covered in this chapter include:
1. The join () is introduced
2. Join () source code analysis (based on jdk1.7.0_40)
3. The join () sample

1. The join () is introduced
Join () is defined in thread.java.
Join () lets the main thread wait for the child thread to finish before it can continue. This sentence may be a bit obscure, but let's try to understand it through examples:


//The main thread
public class Father extends Thread {
    public void run() {
        Son s = new Son();
        s.start();
        s.join();
        ...
    }
}
//The child thread
public class Son extends Thread {
    public void run() {
        ...
    }
}

Description:
There are two classes above, Father(the main thread class) and Son(the child thread class). Because Son is created and started in Father, Father is the main thread class and Son is the child thread class.
In the Father main thread, new "child thread s" is created through new Son(). Next, the "child thread s" is started with s.statart () and s.giovin () is called. After calling s.giovin (), the Father main thread waits until the "child thread s" runs out. The Father main thread cannot continue until the "child thread s" has run. This is what we call the "join() function, which is to make the main thread wait for the child to finish before it can continue running"!

2. Join () source code analysis (based on jdk1.7.0_40)


public final void join() throws InterruptedException {
    join(0);
}
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;
        }
    }
}

Description:
From the code, we can see that. When millis==0, a while(isAlive()) loop is entered. As long as the child thread is alive, the main thread waits.
Let's understand the use of join() based on the code above that explains why join() works!
Question:
While s.giovin () is called in the "Father main thread", s.giovin () is a join() called through the "child thread s". Then, isAlive() in the join() method should be to determine whether "child thread s" isAlive state. The corresponding wait(0) should also be "make child thread s" wait. But if that's the case, how could s.giovin () be "making the main thread wait until the child thread s completes" instead of "making the child thread wait (because it calls the wait method of the child thread object s)"?
Answer: wait() makes the "current thread" wait, and by "current thread" I mean the thread currently running on the CPU. So, although it calls the child thread's wait() method, it does so through the main thread. So, it's the main thread that sleeps, not the "child threads"!

3. The join () sample
Now that you understand what join() does, look at the usage of join() through an example.


//JoinTest. Java source code
public class JoinTest{ 
    public static void main(String[] args){ 
        try {
            ThreadA t1 = new ThreadA("t1"); //New "thread t1"
            t1.start();                     //Start "thread t1"
            t1.join();                        //Add "thread t1" to "main thread main" and "main thread main() will wait for it to complete"
            System.out.printf("%s finishn", Thread.currentThread().getName()); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } 
    static class ThreadA extends Thread{
        public ThreadA(String name){ 
            super(name); 
        } 
        public void run(){ 
            System.out.printf("%s startn", this.getName()); 
            //Delay operation
            for(int i=0; i <1000000; i++)
               ;
            System.out.printf("%s finishn", this.getName()); 
        } 
    } 
}

Operation results:


t1 start
t1 finish
main finish

Results:
The running process is shown in the figure.
(01) create "thread t1" in the" main thread "by new ThreadA("t1"). Next, start "thread t1" with t1.start() and execute t1.join().
(02) after executing t1.join(), the main thread enters a "blocking state" and waits for t1 to finish running. After the "child thread t1" ends, the "main thread" will wake up, and the "main thread" will regain the CPU execution right and continue to run.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201401/20140114110542.jpg? 20140141166 ">


Related articles: