Java join()方法在多线程中的工作原理

Java join()方法在多线程中的工作原理,java,multithreading,Java,Multithreading,根据我对join()方法的理解,它允许一个线程等待另一个线程完成 然后,根据我的代码:只要线程(t0)结束,线程(t3)就应该开始,但这不会发生 public class Threaded extends Thread { @Override public void run() { for (int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName

根据我对join()方法的理解,它允许一个线程等待另一个线程完成

然后,根据我的代码:只要线程(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();

    }

}
在此输出中,线程3在线程0、线程1和线程2结束后开始。 但是,根据我的说法,线程-3在线程-0结束后立即开始

线程-0结束后,线程-3立即开始

线程0完成后,线程3变为可运行。但这并不意味着它是立即安排的。可能同时有3个可运行线程,并且不可预测哪个线程先打印

线程-0结束后,线程-3立即开始

线程0完成后,线程3变为可运行。但这并不意味着它是立即安排的。可能同时有3个可运行的线程,并且无法预测哪个线程先打印。

thread.join()
等待此线程死亡。基本上所有线程都运行在主线程之上。让我试着解释一下代码的流程

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();

}
现在,对于您的问题,您的线程t1、t2和t3都在可运行线程池中。因此,选择当前的线程并执行它完全取决于线程调度程序。

Thread.join()
等待该线程死亡。基本上所有线程都运行在主线程之上。让我试着解释一下代码的流程

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();

}

现在,对于您的问题,您的线程t1、t2和t3都在可运行线程池中。因此,选择当前的线程并执行它完全取决于线程调度器。

您的假设是线程名称是按顺序设置的,这可能是正确的。通过在构造函数中自己设置它们,无论是通过静态计数器还是通过显式传递标签,都可以确保。线程3在线程0结束时立即开始=>这是真的,但它仍然与1和2并行,因此无法确定下一步将执行1、2或3中的哪一个。问题是“尽快”您的程序无法确保
t3.start()
线程一结束就会调用
t0
线程。它只确保在
t0
线程结束之前不会调用
t3.start()
。您可以假设线程名称是按顺序设置的。通过在构造函数中自己设置它们,无论是通过静态计数器还是通过显式传递标签,都可以确保。线程3在线程0结束时立即开始=>这是真的,但它仍然与1和2并行,因此无法确定下一步将执行1、2或3中的哪一个。问题是“尽快”您的程序无法确保
t3.start()
线程一结束就会调用
t0
线程。它只确保在
t0
线程结束之前不会调用
t3.start()