简体   繁体   English

join()方法如何在多线程中工作

[英]how join() method works in multi-threading

According to my understanding of join() method, it allows which allows one thread to wait until another thread completes. 根据我对join()方法的理解,它允许哪个线程等待另一个线程完成。

Then, according to my code: as soon as thread (t0) ends the thread (t3) should start, which doesn't happen 然后,根据我的代码:只要线程(t0)结束,线程(t3)就应该启动,这不会发生

public class Threaded extends Thread {

     @Override
     public void run() {

      for (int i=0; i<5; i++) {

           System.out.println(Thread.currentThread().getName() + ": " + i);

       }

       }

}

public class Demo {

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

        Thread main = Thread.currentThread();

        Threaded t0 = new Threaded();
        Threaded t1 = new Threaded();
        Threaded t2 = new Threaded();
        Threaded t3= new Threaded();

        t0.start();
        t1.start();
        t2.start();

        t0.join();

        t3.start();

    }

}

The output is 输出是

main 5  
Thread-1: 0  
Thread-2: 0  
Thread-0: 0  
Thread-2: 1  
Thread-2: 2  
Thread-1: 1  
Thread-2: 3  
Thread-2: 4  
Thread-0: 1  
Thread-0: 2  
Thread-0: 3  
Thread-0: 4  
Thread-1: 2  
Thread-1: 3  
Thread-1: 4  
Thread-3: 0  
Thread-3: 1  
Thread-3: 2  
Thread-3: 3  
Thread-3: 4

In this output the Thread-3 starts after Thread-0, Thread-1 and Thread-2 ends. 在此输出中,线程3在线程0,线程1和线程2结束之后开始。 But, according to me Thread-3 starts as soon as the Thread-0 ends. 但是,据我所知,线程0结束后,线程3即开始启动。

Thread-3 starts as soon as the Thread-0 ends. 线程0结束后,线程3开始启动。

Thread-3 becomes runnable after Thread-0 finished. 线程0完成后,线程3变为可运行。 But this does not mean it is scheduled immediatly. 但这并不意味着立即安排。 There might be 3 runnable threads at the same time and it's unpredictable which thread print first. 可能同时有3个可运行线程,并且无法确定哪个线程先打印。

Thread.join() waits for this thread to die. Thread.join()等待该线程死亡。 Basically all Threads runs on top of main thread. 基本上所有线程都在主线程上运行。 So let me try to explain the flow on your code. 因此,让我尝试解释您的代码流程。

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

    Thread main = Thread.currentThread();

    // you have created 4 Thread instances here...
    Threaded t0 = new Threaded();
    Threaded t1 = new Threaded();
    Threaded t2 = new Threaded();
    Threaded t3= new Threaded();

    // you have started t0, t1 and t2 to run on top of main thread
    t0.start();
    t1.start();
    t2.start();

    // here you have used Thread.join()
    // so your main thread will wait here, 
    // it will wait for the completion of t0 
    t0.join();

    // so after the completion of t0, t3 will start
    t3.start();

}

So now for your question, your threads t1, t2 and t3 all are in runnable Thread pool. 现在,对于您的问题,您的线程t1,t2和t3都在可运行的线程池中。 So, it is entirely in the hand of Thread scheduler to pick which thread at the moment and execute. 因此,现在选择哪个线程并执行完全在Thread Scheduler手中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM