Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未在运行()中打印子线程名称_Java_Multithreading - Fatal编程技术网

Java 未在运行()中打印子线程名称

Java 未在运行()中打印子线程名称,java,multithreading,Java,Multithreading,我有以下问题。正如您在代码(main()内部)中看到的,我将子线程的名称设置为“Child1”和“Child2”。因此,当这两个子线程运行run()方法时,我试图打印它们的名称。但正如您从输出中看到的,“Child2”线程的名称没有打印出来 请告诉我为什么会这样。代码有问题吗 package threads_concurrency; class MyRunnable2 implements Runnable { public void run() { for(i

我有以下问题。正如您在代码(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
包线程\u并发;
类MyRunnable2实现Runnable
{
公开募捐
{

对于(inti=1;i对于线程2,您使用的是
MyRunnable
类,而不是
MyRunnable2

MyRunnable mr2=new MyRunnable();
将其更改为MyRunnable2,它应该可以工作。

问题就在这里

MyRunnable mr2=new MyRunnable();
你应该

MyRunnable2 mr2=new MyRunnable2();

要获得您期望的结果:)

提示:学习java命名约定……不,在java名称中!在您的示例中,您使用了两个不同的类。
MyRunnable
MyRunnable2
@Janoz好点-这就是为什么我们应该考虑名称而不仅仅是附加数字,因为这些差异很难发现。显示
MyRunnable
的代码或您使用的
MyRunnable
是不是搞错了?