子线程名称未在run()

时间:2016-06-28 10:57:07

标签: 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 个答案:

答案 0 :(得分:0)

对于主题2,您使用MyRunnable课程代替MyRunnable2

MyRunnable mr2=new MyRunnable();

将其更改为MyRunnable2,它应该有效。

答案 1 :(得分:0)

问题出在这里

MyRunnable mr2=new MyRunnable();

你应该

MyRunnable2 mr2=new MyRunnable2();

得到你的期望:)