The underlying principle of the join method

Let me talk about the conclusion first:

        Join can add the specified thread to the current thread, and merge two alternately executed threads into sequentially executed threads. For example, if the Join() method of thread B is called in thread A, thread A will not continue to execute until thread B finishes executing.

        

 

        The underlying implementation is based on the object's wait and notify methods. When a thread A executes the join() method of another thread B, because the join method is modified by the synchronized keyword, thread A will obtain the object lock of thread B, and then execute the wait method of thread B at the bottom layer, then Thread A will release the lock and enter the waiting pool of thread B. When thread B finishes executing, the underlying JVM will automatically call the notifyAll() method of the thread B object to notify all threads waiting for the object to be locked, including thread A. At this time, thread A will re-enter the ready state, waiting to obtain CPU resources and continue to execute downward.
 

        Analysis of the code on the figure: First, two thread objects are created, namely: testA1 and testA2, and then the join method of testA1 is called in the main thread, then the main thread will acquire the object lock of testA1, and then testA1 will be called at the bottom wait method, at this time the main thread will release the object lock of testA1, then it will enter the blocking state and enter the waiting pool of testA1, then there is no way to execute the testA2.start method, and then it can only wait for the testA1 thread to finish executing. After the testA1 thread is executed, it will call the notifyall method of the testA1 object in the jvm virtual machine to wake up all threads waiting for the object lock. At this time, the main thread will be awakened, and then execute the testA2.start method

1. Sample code:

public class test {

    public static void main(String[] args) throws InterruptedException {

        testA testA1 = new testA();
        testA testA2 = new testA();

        testA1.start();
        testA2.start();
    }

    static class testA extends Thread{

        @Override
        public void run() {

            for (int i = 0; i < 100; i++) {
                System.out.println("名称为:" + Thread.currentThread().getName() + "的线程:" + i);
            }

        }

    }
}

operation result:

         You can see that the threads are executed synchronously, but if you want to wait for the execution of the A1 thread to complete before executing the A2 thread, you can use the join method of the thread

2. Add join method sample code:

public class test {

    public static void main(String[] args) throws InterruptedException {

        testA testA1 = new testA();
        testA testA2 = new testA();

        testA1.start();
        // 在main方法中执行 testA1.join(); 所以当前线程是 主线程
        testA1.join();
        testA2.start();
    }

    static class testA extends Thread{

        @Override
        public void run() {

            for (int i = 0; i < 100; i++) {
                System.out.println("名称为:" + Thread.currentThread().getName() + "的线程:" + i);
            }

        }

    }
}

operation result:

 

Guess you like

Origin blog.csdn.net/qq_26112725/article/details/131326689