简体   繁体   English

让一个线程等待另一个线程完成

[英]Make one thread wait for another to finish

I have two thread classes: one that prints numbers from 0 to 9, and another from 100 to 109. What I want is to make the first thread wait for the other one to finish. 我有两个线程类:一个打印从0到9的数字,另一个打印从100到109的数字。我要使第一个线程等待另一个线程完成。 For this, I used the join() method, but it's not working. 为此,我使用了join()方法,但是它不起作用。 Please tell me where I'm going wrong: 请告诉我我要去哪里了:

//demonstrates the use of join() to wait for another thread to finish
class AThread implements Runnable {
    Thread t;

    AThread() {
        t = new Thread(this);
    }

    public void run() {
        try {
            for (int i=0; i<10; i++) {
                System.out.println(i);
                Thread.sleep(10);
            }
        } catch (InterruptedException e) {
            System.out.println(t + " interruped.");
        }
    }

    public void halt(Thread th) {
        try {
            th.join();
        } catch (InterruptedException e) {
            System.out.println(t + " interruped.");
        }
    }
}

//a different thread class (we distinguish threads by their output)
class BThread implements Runnable {
    Thread t;

    BThread() {
        t = new Thread(this);
    }

    public void run() {
        try {
            for (int i=100; i<110; i++) {
                System.out.println(i);
                Thread.sleep(10);
            }
        } catch (InterruptedException e) {
            System.out.println(t + " interruped.");
        }
    }
}

public class WaitForThread {
    public static void main(String[] args) {
        AThread t1 = new AThread();
        BThread t2 = new BThread();

        t1.t.start();
        t1.halt(t2.t); //wait for the 100-109 thread to finish
        t2.t.start();
    }
}

You call join on the thread before it has started. 您可以在线程启动之前在该线程上调用join That doesn't work; 那行不通; in that case, join will return immediately, it's not going to wait until the other thread has started and stopped later. 在这种情况下, join将立即返回,而不必等到另一个线程启动和稍后停止。 You can see this in the API documentation: 您可以在API文档中看到以下内容:

Thread.join()

This implementation uses a loop of this.wait calls conditioned on this.isAlive . 此实现使用的循环this.wait电话空调上this.isAlive

Thread.isAlive()

Tests if this thread is alive. 测试此线程是否仍然存在。 A thread is alive if it has been started and has not yet died. 如果线程已经启动但尚未死亡, 则该线程是活动的。

Reorder the statements in your main method main方法中对语句重新排序

t1.t.start();
t2.t.start();
t1.halt(t2.t); //wait for the 100-109 thread to finish

edit to answer your questions in the comments: 编辑以在评论中回答您的问题:

If you want the thread in AThread to wait for the thread in BThread to finish before doing its job, then you'll need to call join in AThread.run , and change your main method: 如果你想在线程AThread等待的线程BThread做的工作之前完成,那么你就需要调用joinAThread.run ,并改变你的main方法:

class AThread implements Runnable {
    Thread t;
    Thread threadToWaitFor;

    AThread(Thread threadToWaitFor) {
        t = new Thread(this);
        this.threadToWaitFor = threadToWaitFor;
    }

    public void run() {
        // First wait for the other thread to finish
        threadToWaitFor.join();

        // ...
    }

    // ...
}

public class WaitForThread {
    public static void main(String[] args) {
       BThread t2 = new BThread();
       AThread t1 = new AThread(t2.t);

        t2.t.start();
        t1.t.start();
    }
}

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

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