如何获得第一个线程-0java?

csga3l58  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(341)

我正在做我的工作坊,问题如下:

倒车喂

编写一个名为reversehello.java的程序来创建一个线程(我们称之为thread1)。
线程1创建另一个线程(线程2);线程2创建线程3;以此类推,直到线程50。
每个线程都应该打印“hello from thread!”,但是你应该构造你的程序,让线程以相反的顺序打印他们的问候语。
我的代码如下:

public class Task2 implements Runnable {

        static int threadNo = 1;

        @Override
        public void run() {
            if (threadNo <= 50) {
                Thread reverse = new Thread(new Task2());
                reverse.setName("Thread "+ threadNo);
                threadNo++;
                reverse.start();
                try {
                    reverse.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("hello from " + Thread.currentThread().getName());
        }

        public static void main(String[] args) throws InterruptedException {
            Thread t2 = new Thread(new Task2());
            t2.start();
        }

}

输出如下:

hello from Thread 50
hello from Thread 49
hello from Thread 48
hello from Thread 47
hello from Thread 46
hello from Thread 45
hello from Thread 44
hello from Thread 43
hello from Thread 42
hello from Thread 41
hello from Thread 40
hello from Thread 39
hello from Thread 38
hello from Thread 37
hello from Thread 36
hello from Thread 35
hello from Thread 34
hello from Thread 33
hello from Thread 32
hello from Thread 31
hello from Thread 30
hello from Thread 29
hello from Thread 28
hello from Thread 27
hello from Thread 26
hello from Thread 25
hello from Thread 24
hello from Thread 23
hello from Thread 22
hello from Thread 21
hello from Thread 20
hello from Thread 19
hello from Thread 18
hello from Thread 17
hello from Thread 16
hello from Thread 15
hello from Thread 14
hello from Thread 13
hello from Thread 12
hello from Thread 11
hello from Thread 10
hello from Thread 9
hello from Thread 8
hello from Thread 7
hello from Thread 6
hello from Thread 5
hello from Thread 4
hello from Thread 3
hello from Thread 2
hello from Thread 1
hello from Thread-0

我不知道最后一个“hello from thread-0”是怎么来的,也不知道如何去掉这个,因为这个问题只从线程1到50问。谢谢您

zhte4eai

zhte4eai1#

从你的第一个线程开始 main 方法从不获取线程名称集,因此它将具有由jvm分配的默认名称,结果是 "Thread-0" .
一个简单的解决方案是在 run 方法,而不是在已启动的 reverse 线程:

@Override
public void run() {
    Thread.currentThread().setName("Thread "+ threadNo);
    if (threadNo <= 50) {
tzxcd3kk

tzxcd3kk2#

Thread-0 创建并开始的初始线程的名称 main 方法而不增加中的线程计数器 Task2 .
因此,要解决此问题,启动初始线程就足够了:

new Thread(new Task2(), "Thread "+ Task2.threadNo++).start();
sbtkgmzw

sbtkgmzw3#

这段代码应该能解决你的问题。

public class Task implements Runnable {

    private final int threadNo;

    public Task(int threadNo) {
        this.threadNo = threadNo;
    }

    @Override
    public void run() {
        if (threadNo <= 50) {
            Thread reverse = new Thread(new Task(threadNo + 1));
            reverse.setName("Thread " + threadNo);
            reverse.start();
            try {
                reverse.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (threadNo > 1) {
            System.out.println("hello from " + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        Thread t2 = new Thread(new Task(1));
        t2.start();
    }
}

相关问题