join() method in multithreaded Thread

join() method in multithreaded Thread

As we all know, the join() method adds thread B to thread A, and A blocks and waits for B to finish running. However, if you call multiple joins and add multiple sub threads in A at A time, will it become A single thread mode (sequential execution). Let's take A look at the specific analysis of the join method.
This is the description of the join() method in the API. This description is as short as a ghost. I can't understand it

This is the comment of the source code

Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

Baidu translation is

The maximum time to wait for this thread to die is milliseconds. Timeout 0 means waiting forever.
This implementation uses this The loop called by wait, which starts with this Isalive is a condition. This. Is called when the thread terminates NotifyAll method. It is recommended that applications do not use wait, notify, or notifyAll on thread instances.

The translation is a little inaccurate. If your English is not good, make do with it. Its timeout of 0 means to wait forever. In fact, it means that the method of join(long millis) passes a millisecond. If it passes 0, it will wait until the sub thread dies.

Let's look at the specific implementation:

public class Test01 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadDemo t1 = new MyThreadDemo("Thread 1111");
        MyThreadDemo t2 = new MyThreadDemo("Thread 2222");
        MyThreadDemo t3 = new MyThreadDemo("Thread 333");
        MyThreadDemo t4 = new MyThreadDemo("Thread 4");
        //Four threads start and four threads start to grab resources
        t1.start();
        t2.start();
        t3.start();
        t4.start();

        //This will cause the main thread to block until t1 is completed
        //t1 and t2 are not executed in sequence
        //This does not mean that only t1 and t2 will rob resources, but t3 and t4 will rob resources as well
        t1.join();
        //After t1 is executed, the main thread enters the ready state, starts to grab resources and continues to go down
        
        //Add t2, if not completed
        //main blocking
        t2.join();
        //After t2 execution, the main thread main enters the ready state and starts to grab resources

        //This sentence must be executed after t1 and t2
        //It will compete for resources with t3 and t4 that have not yet been implemented,
        // (it is also possible that t3 and t4 are executed before t1 and t2 are executed, so there is no resource grabbing, and only one foreground thread is left in main
        //Note: the garbage collector is a background thread (daemon thread). As long as there are threads, the garbage collector will not die
        System.out.println("++++++++++++++++++");

        //Here, if t3 and t4 have not been executed, the main thread will block and let t3 and t4 execute first
        //If the execution is completed before, skip the join
        t3.join();
        t4.join(); 
        System.out.println("--------------------");
    }
}
public class MyThreadDemo extends Thread{

    public MyThreadDemo(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

Finally, to sum up, the join method is that thread A joins A thread B, and thread A enters the blocking state. After thread B enters the CPU, it will be kicked out after the time slice is over. It is not necessarily thread B that grabs resources next time, (thread A is blocked and won't grab) it may also be threads C, D, E, etc. thread A enters the ready state and starts to grab resources until thread B runs. Thread A grabs resources before executing the next statement after the join method. If the next statement is the join method, judge whether the thread calling the join is dead or not, skip it if it is dead, and add it if it is not dead. (generally speaking, thread A here is the main thread main)
Successive calls to join() do not execute these threads sequentially.
Note: This article does not cover processes. If you accidentally make a mistake, please change to threads automatically

Keywords: Java Multithreading

Added by gfadmin on Sun, 26 Dec 2021 22:27:23 +0200