java - 子线程名称未在run()中打印

标签 java multithreading

我有以下问题。正如您在代码中看到的那样(在main()内部),我将子线程的名称设置为“Child1”和“Child2”。因此,当这两个子线程正在运行run()方法时,我尝试打印其名称。但是,正如您从输出中看到的,“Child2”线程的名称未得到打印。

请告诉我为什么会这样。代码有什么问题吗?

package threads_concurrency;

class MyRunnable2 implements Runnable
{
    public void run()
    {
        for(int i=1;i<21;i++)
            System.out.println("Child Thread "+Thread.currentThread().getName());
        try
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException ie)
        {
            System.out.println("child thread got interrupted");
        }

    }
}

public class NameIdPriorityValuesOfThread 
{

    public static void main(String[] args) 
    {
    Thread main=Thread.currentThread();
    System.out.println("id of main thread = "+main.getId());
    System.out.println("name of main thread = "+main.getName());
    System.out.println("priority of main thread = "+main.getPriority());

    System.out.println("==================================");

    //Code to create thread1
    MyRunnable2 mr1=new MyRunnable2();
    Thread t1=new Thread(mr1);
    System.out.println("default id of t1 is :"+t1.getId());
    System.out.println("default name of t1 is :"+t1.getName());
    System.out.println("default priority of t1 is :"+t1.getPriority());
    t1.setName("Child1"); t1.setPriority(9);

    System.out.println("==================================");

    //Code to create thread2
    MyRunnable mr2=new MyRunnable();
    Thread t2=new Thread(mr2);
    System.out.println("default id of t2 = "+t2.getId());
    System.out.println("default name of t2 = "+t2.getName());
    System.out.println("default priority of t2 = "+t2.getPriority());
    t2.setName("Child2"); t2.setPriority(9);
    System.out.println("==================================");

    t1.start();
    t2.start();

    for(int i=1;i<21;i++)
        System.out.println("main thread");

    }

}

*************OUTPUT*****************
id of main thread = 1
name of main thread = main
priority of main thread = 5
==================================
default id of t1 is :8
default name of t1 is :Thread-0
default priority of t1 is :5
==================================
default id of t2 = 9
default name of t2 = Thread-1
default priority of t2 = 5
==================================
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
main thread
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
Child Thread Child1
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread
main thread

最佳答案

对于线程2,您使用的是MyRunnable类,而不是MyRunnable2

MyRunnable mr2=new MyRunnable();

将其更改为MyRunnable2,它应该可以工作。

关于java - 子线程名称未在run()中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074298/

相关文章:

c++ - 如何安全地从一个线程读取变量并从另一个线程修改它?

.net - 我认为我正在处理的未处理的 FileLoadException

java - Swing 将对话框绑定(bind)到 JButton

java - 将 jars 添加到 java webapp

java - 支柱 1 : How may I pass data between Actions?

java - boolean 和 == 与 =

multithreading - Azure 服务 TopicClient 线程安全且可重用吗?

android - 等待 TextToSpeech onInit() 初始化

使用 Kotlin 协程的多线程

Java插件框架选择