2016-06-28 1 views
2

다음과 같은 문제가 있습니다. } 코드에서 볼 수 있듯이 (main() 내부에서) 하위 스레드의 이름을 "Child1"및 "Child2"로 설정합니다. 그래서이 2 개의 자식 스레드가 run() 메소드를 실행할 때 그 이름을 출력하려고합니다. 하지만 출력에서 ​​볼 수 있듯이 "Child2"스레드의 이름이 인쇄되지 않습니다.run() 내에서 하위 스레드 이름이 인쇄되지 않습니다.

왜 이런 일이 발생하는지 알려주십시오. 코드에 문제가 있습니까?

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 
+1

힌트를 기대했던 얻을

MyRunnable2 mr2=new MyRunnable2(); 

을 가져야 여기

MyRunnable mr2=new MyRunnable(); 

입니다! 귀하의 예제에서 – GhostCat

+2

당신은 2 개의 다른 클래스를 사용하고 있습니다. ''MyRunnable''과''MyRunnable2''가 있습니다. – Janoz

+0

@Janoz 좋은 장소 - 그 이유는 자리를 밝히기가 쉽지 않기 때문에 이름에 대해 생각하고 번호를 추가하는 것이 아닙니다. – Thomas

답변

0

는 스레드 2의 대신 MyRunnable2-MyRunnable2

MyRunnable mr2=new MyRunnable(); 

변경을의 MyRunnable 클래스를 사용하고 그것은 작동합니다. 자바 이름에 _ 아니 ... 연구 자바 명명 규칙 :

0

문제는 당신이 :)

관련 문제