gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:09:54 25 4
gpt4 key购买 nike

我有以下问题。正如您在代码中看到的那样(在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com