The role of the thread join() method

First, the role of join thread

Join() method: Thread provides a thread to wait for another thread to complete. When a join method (t2.join()) of another thread (such as a t2 thread) is called in a program execution flow (such as the main thread), the calling thread (main thread) will be blocked until it is added by the join() method. The join thread (t2.start()) is executed.

Second, the display code analysis

1, when there is no join method

    public class MyJoinTest implements Runnable {
    
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "====" + i);
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            MyJoinTest myJoinTest = new MyJoinTest();
                         Thread t1 = new Thread(myJoinTest, "normal thread");
                         Thread t2 = new Thread(myJoinTest, "thread of join");
                         T1.start(); //Start t1 thread
    
                         / / Below belongs to the main main thread
            for (int i = 0; i < 10; i++) {
                if (i == 5) {
                                         T2.start(); //Start t2 thread
                }
                System.out.println(Thread.currentThread().getName() + "====" + i);
            }
        }
    }

The output is
main====0
main====1
main====2
main====3
main====4
normal thread ====0
normal thread ====1
normal thread ====2
Ordinary thread ====3
Normal Thread ====4
main====5
main====6
main====7
main====8
main====9
join thread ====0
join thread ====1
join thread ====2
join thread ====3
join thread ====4

2, when there is a join method

    public class MyJoinTest implements Runnable {
    
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "====" + i);
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            MyJoinTest myJoinTest = new MyJoinTest();
            Thread t1 = new Thread(myJoinTest, "normal thread");
                         Thread t2 = new Thread(myJoinTest, "thread of join");
                         T1.start(); //Start t1 thread
    
                         / / Below belongs to the main main thread
            for (int i = 0; i < 10; i++) {
                if (i == 5) {
                                         T2.start(); //Start t2 thread
                                         //The main thread calls the join method of the t2 thread, causing the main thread to wait for the end of t2 execution before it can execute downward.
                    t2.join();
                }
                System.out.println(Thread.currentThread().getName() + "====" + i);
            }
        }
    }

The output is
main====0
main====1
main====2
main====3
main====4
normal thread ====0
normal thread ====1
normal thread ====2
Ordinary thread ====3
Normal Thread ====4
join thread ====0
join thread ====1
join thread ====2
join thread ====3
join thread ====4
main====5
main====6
main====7
main====8
main====9

3, the source code of the join () method

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;
            }
        }
    }

From the source code, you can see that the principle of the join method is to call the wait method of the corresponding thread to wait for operation. For example, the join method of the B thread is called in the A thread, which is equivalent to calling the wait method of the B thread in the A thread. When the B thread finishes executing (or arrives at the waiting time), the B thread will automatically call its own notifyAll method to wake up the A thread, thus achieving the purpose of synchronization.

Intelligent Recommendation

Thread join() method

Introduction to join The join() method is a method in the Thread class whose definition is to wait for the thread to terminate. In fact, the join() method will suspend the execution of the calling thr...

Thread join () method parses

java.lang.Threadofjoin()Methods Source follows: The first method signature is _synchronized_。 Holds the current thread (ta) Object lock thread (tb) Callingta.join()Methods thread _tb_ will perform at ...

Thread join () method

Multithreading join (), and there can be no-argument argument, join (long mills) .join is to provide a method of Thread, join the main method used to achieve this thread when death is the beginning of...

Thread the join () method

Disclaimer: This article is CSDN blogger "2 to the power of 32" in the original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and t...

On Java thread join () method

Role 1.join () of When you call a thread's join () method, which suspends the calling thread until the end of the execution of the calling thread, the calling thread will continue to execute. 2. Put j...

More Recommendation

Thread method join

xl_echo editing and finishing, welcome to reprint, please declare the source of the article. For more IT, programming cases, and materials, please contact QQ: 1280023003, Jiaqun 298140694. Undefeated ...

Thread: the use of the method join

The role of the method join () is to wait for the thread object to be destroyed. The function of the method join () is that the thread object x belongs to normally executes the task in the run () meth...

Multi-thread join method

The role of join is to let other threads become waiting, t1.join();// Let other threads become waiting, and not release until the current t1 thread finishes executing. thread.Join adds the specified t...

The join() method in the Thread class

In many cases, the main thread creates and starts a child thread. If a large amount of time-consuming operations are performed in the child thread, the main thread will end before the child thread. At...

The join() method in Thread

As for whether the "lock ID" will be released by the join() method, it can be considered that, under the premise of whether there is a synchronized keyword, it can be roughly divided into fo...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top