需要程序的输出,t1线程引用的是工作类,那么输出是工作5次,然后是rest吗?

n8ghc7c1  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(131)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

20小时前关门了。
改进这个问题
我正在学习java并尝试一些多线程的概念,我在ide上尝试过这个,但是输出正在改变。我想知道下面程序的输出-

class Work implements Runnable {
    public void run() {
        for (int i = 0; i < 5; i++) {
            String tn = Thread.currentThread().getName();
            System.out.println(tn);
        }

        String tn = Thread.currentThread().getName();
        if (tn.equals("REST")) {
            try {
                Thread.currentThread().join();
            } catch (Exception ex) {}
        }
    }
}

class Rest implements Runnable {
    public void run() {
        for (int i = 0; i < 5; i++) {
            String tn1 = Thread.currentThread().getName();
            System.out.println(tn1);
        }
    }
}

public class test1 {
    public static void main(String[] args) {
        Work w = new Work();
        Rest r = new Rest();
        Thread t1 = new Thread(w, "WORK");
        Thread t2 = new Thread(r, "REST");
        t1.start();
        t2.start();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题