简体   繁体   English

Thread.join无法正常工作

[英]Thread.join not working

A call to join() Guaranteed to cause the current thread to stop executing until the thread it joins with (in other words, the thread it calls join() on) completes. 对join()的调用保证使当前线程停止执行,直到它加入的线程(换句话说,它调用join()的线程)完成。

However, in my program both the threads are executing simultaneously. 但是,在我的程序中,两个线程同时执行。 Thread1 is not waiting for Thread2 to finish its execution. Thread1没有等待Thread2完成执行。

What is wrong with my program? 我的计划有什么问题?

public class Launcher1 {


    public static void main(String[] args) {
        JoinExample runnable=new JoinExample();

        Thread thread1=new Thread(runnable);
        Thread thread2=new Thread(runnable);

        thread1.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        thread2.start();
        try {
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

public class JoinExample implements Runnable{
    public void run(){
        for(int i=0;i<10;i++){
            System.out.println("i:"+i+" "+Thread.currentThread().getName());

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

Output: 输出:

i:0 Thread-0 i:0线程-0

i:1 Thread-0 我:1个线程-0

i:0 Thread-1 i:0线程-1

i:2 Thread-0 i:2个线程-0

i:1 Thread-1 我:1个线程-1

i:3 Thread-0 我:3个线程-0

i:4 Thread-0 我:4个线程-0

i:2 Thread-1 我:2个线程-1

i:3 Thread-1 我:3线程-1

i:5 Thread-0 我:5个线程-0

i:4 Thread-1 我:4线程-1

i:6 Thread-0 我:6个线程-0

i:7 Thread-0 我:7个线程-0

i:5 Thread-1 我:5线程-1

i:8 Thread-0 我:8个线程-0

i:6 Thread-1 我:6线程-1

i:7 Thread-1 我:7线程-1

i:9 Thread-0 我:9线程-0

i:8 Thread-1 我:8个线程-1

i:9 Thread-1 我:9线程-1

However, in my program both the threads are executing simultaneously. 但是,在我的程序中,两个线程同时执行。 Thread1 is not waiting for Thread2 to finish its execution. Thread1没有等待Thread2完成执行。

No, and it wouldn't - because thread 1 isn't calling join . 不,它不会 - 因为线程1没有调用join Look at the docs you quoted again: 看看你再次引用的文档:

A call to join() Guaranteed to cause the current thread to stop executing until the thread it joins with 对join()的调用保证使当前线程停止执行,直到它加入的线程

To put it another way: the join() method doesn't finish until the thread it's called on has finished. 换句话说: join()方法在调用它的线程完成之前不会完成。

You've got three threads in your program - two that are running JoinExample.run , and the "main" thread that starts the other two. 你的程序中有三个线程 - 两个运行JoinExample.run ,以及启动另外两个的“main”线程。 You're telling that main thread to wait for thread 2 to finish, but that's all. 你告诉线程等待线程2完成,但这就是全部。 Your two JoinExample threads are entirely independent. 您的两个JoinExample线程完全独​​立。

There are three threads in question here. 这里有三个问题。 1st thread - main thread, which runs the main method and spawns the 2 child threads. 第一个线程 - 主线程,它运行main方法并生成2个子线程。 2nd thread - thread1 3rd thread - thread2 When you say thread2.join() , the main thread will block until thread2 finishes, thread1 and thread2 will continue to run. 第二个线程 - thread1第三个线程 - thread2当你说thread2.join() ,主线程将阻塞直到thread2完成, thread1thread2将继续运行。

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

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