了解 join() 方法示例

发布于 2024-11-04 11:20:18 字数 969 浏览 1 评论 0原文

Java 线程 join() 方法让我有点困惑。我有以下示例

class MyThread extends Thread {
    private String name;
    private int sleepTime;
    private Thread waitsFor;

    MyThread(String name, int stime, Thread wa) { … }

    public void run() {
        System.out.print("["+name+" ");

        try { Thread.sleep(sleepTime); }
        catch(InterruptedException ie) { }

        System.out.print(name+"? ");

        if (!(waitsFor == null))
        try { waitsFor.join(); }
        catch(InterruptedException ie) { }

        System.out.print(name+"] ");

线程

public class JoinTest2 {
    public static void main (String [] args) {
        Thread t1 = new MyThread("1",1000,null);
        Thread t2 = new MyThread("2",4000,t1);
        Thread t3 = new MyThread("3",600,t2);
        Thread t4 = new MyThread("4",500,t3);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

按什么顺序终止?

The Java thread join() method confuses me a bit. I have following example

class MyThread extends Thread {
    private String name;
    private int sleepTime;
    private Thread waitsFor;

    MyThread(String name, int stime, Thread wa) { … }

    public void run() {
        System.out.print("["+name+" ");

        try { Thread.sleep(sleepTime); }
        catch(InterruptedException ie) { }

        System.out.print(name+"? ");

        if (!(waitsFor == null))
        try { waitsFor.join(); }
        catch(InterruptedException ie) { }

        System.out.print(name+"] ");

And

public class JoinTest2 {
    public static void main (String [] args) {
        Thread t1 = new MyThread("1",1000,null);
        Thread t2 = new MyThread("2",4000,t1);
        Thread t3 = new MyThread("3",600,t2);
        Thread t4 = new MyThread("4",500,t3);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

In which order are the threads terminated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺ぐ爱和你が 2024-11-11 11:20:46

线程终止的顺序肯定是从 t1、t2 开始,依此类推。也许你也会从这里清楚地了解java线程中的join()方法 http://findnerd.com/list/view/Java-Thread-Join-Example/4465/

Surely the order of thread termination would be from t1, t2, and so on here. May be you will get a clear understanding of join() method in java thread from here also http://findnerd.com/list/view/Java-Thread-Join-Example/4465/

離殇 2024-11-11 11:20:45

它将按 t1、t2、t3、t4... 的顺序终止。 join 导致当前正在执行的线程等待,直到调用它的线程终止。

It would terminate in order t1, t2, t3, t4... join causes the currently executing thread to wait until the thread it is called on terminates.

灯下孤影 2024-11-11 11:20:45

有一个主线程启动您创建的四个自定义线程 t1、t2、t3 和 t4。现在,当在线程上调用 join() 方法并且未提供参数(时间)时(默认情况下它为 0,意味着最大等待时间为永远),则调用线程将等待调用 join 的线程终止。

当 t1.start() 被调用时,JVM 会调用相应的 run() 方法。由于 t1 的 waitsFor 参数为 null,因此它将通过打印其名称来终止。由于您要添加睡眠,因此 t2、t3、t4 将在 t1 完成其睡眠时启动。现在 t2 将等待 t1 终止,因为我们从 t2 调用 t1 上的 join。类似地,t3 将等待 t2,t4 将等待 t3。所以你的执行将是t1->t2->t3->t4

但是,如果 t1 在 t2 对其调用 join 之前终止,则 join 将简单地返回,因为此时(当 t2 对 t1 调用 join 时)isAlive() 将为 false

There is a main thread which starts the four customs threads you created t1,t2,t3 and t4. Now when join() method is invoked on a thread and no argument(time) is supplied(which makes it 0 by default meaning maximum wait time is forever) then the calling thread will wait for the thread on which join was invoked to terminate.

When t1.start() is called it's corresponding run() method is invoked by the JVM. Since waitsFor argument is null for t1 it will simply terminate by printing it's name. Since you are adding sleep t2,t3,t4 would be started by the time t1 completes it's sleep. Now t2 will wait for t1 to terminate as we are calling join on t1 from t2. Similarly t3 will wait for t2 and t4 will wait for t3. So youe execution will go t1->t2->t3->t4.

However if t1 terminates before t2 calls join on it, join will simply return because at that point(when t2 calls join on t1) isAlive() will be false.

笑红尘 2024-11-11 11:20:36

Thread.join() 究竟是什么让您感到困惑?你没有提到任何具体的事情。

鉴于 Thread.join() (如 文档指出),等待该线程终止,然后t4将等待t3完成,这将等待 t2 完成,这将等待 t1 完成。

因此,t1 将首先完成,然后是 t2t3t4

What actually confuses you about Thread.join()? You haven't mentioned anything specific.

Given that Thread.join() (as the documentation states), Waits for this thread to die, then t4 will wait for t3 to complete, which will wait for t2 to complete, which will wait for t1 to complete.

Therefore t1 will complete first, followed by t2, t3, and t4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文