java - 如何到达第一个Java线程0?

标签 java multithreading

我正在做我的研讨会,问题如下:

Reverse hello

Write a program called ReverseHello.java that creates a thread (let’s call it Thread 1).

Thread 1 creates another thread (Thread 2); Thread 2 creates Thread 3; and so on, up to Thread 50.

Each thread should print “Hello from Thread !”, but you should structure your program such that the threads print their greetings in reverse order.

我的代码是这样的:

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。

最佳答案

您从 main 开始的第一个线程方法永远不会获取线程名称集,因此它将有一个由 JVM 分配的默认名称,结果是 "Thread-0" .

一个简单的解决方案是将线程名称设置在 run 开头当前线程上的方法,而不是启动的 reverse线程:

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

关于java - 如何到达第一个Java线程0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64995429/

相关文章:

java - 如果我只想使用注释进行开发,我是否需要 *********ApplicationContext 类?

java - Android 多 dex 项目设置

java - 使用java进行 session 管理

java - 两个线程在不同步的情况下访问的计数器的最小值

mysql - Ruby 中的并行 mysql I/O

c - 从线程打印时值不一致

java - 如何使用 commons-digester 将 XML 文件表示为 Java 对象?

java - 计算多个表中的实体数量

java - 多线程Java

c++ - 我们什么时候应该选择其他IPC而不是直接内存访问来进行线程间通信