2012-04-30 6 views
3

방금 ​​주 스레드를 포함한 세 개의 스레드로 카운트 다운 앱을 만들었습니다. CountdownEven을 낮게 설정하면 countdownOdd가 먼저 표시되지만 출력에서는 아무 것도 일어나지 않습니다. 누구든지 문제를 볼 수 있습니까?우선 순위 설정 스레드

//Main 
public class CountdownApp 
{ 

    public static void main(String[] args) 
    { 
    new CountdownApp().start(); 

    } 
    public void start() 
    { 
     Thread count1 = new CountdownEven(); 
     Thread count2 = new CountdownOdd(); 
     count1.setPriority(Thread.MIN_PRIORITY); 
     count2.setPriority(Thread.MAX_PRIORITY); 
     count1.start(); 
     count2.start(); 
    } 

} 


public class CountdownEven extends Thread 
{ 
    public void run() 
    { 
     for(int i = 10; i > 0; i-=2) 
     { 
      System.out.println(this.getName()+ " Count: " +i); 
      Thread.yield();//This is to allow the other thread to run. 
    } 
    } 


} 

public class CountdownOdd extends Thread 
{ 
    public void run() 
    { 
     for(int i = 9; i > 0; i-=2) 
     { 
      System.out.println(this.getName()+ " Count: " +i); 
      Thread.yield();//This is to allow the other thread to run. 
    } 
    } 

} 
+3

를 참조하십시오. –

+0

방금 ​​코드를 실행했는데'Thread-1 Count : 9' /'Thread-1 Count : 7' /'Thread-1 Count : 5' 등이 있습니다. – assylias

+1

우선 순위를 사용하는 것이 올바른 정의 방법이 아닙니다. 스레드가 실행되는 순서. – assylias

답변

2

나는 코드를 시도 했으므로 결과물을 출력합니다.

Thread-0 Count: 10 
Thread-0 Count: 8 
Thread-0 Count: 6 
Thread-0 Count: 4 
Thread-0 Count: 2 
Thread-1 Count: 9 
Thread-1 Count: 7 
Thread-1 Count: 5 
Thread-1 Count: 3 
Thread-1 Count: 1 

정확해야 출력이 ... 그래서 whats prob 무엇입니까? 어쩌면 당신은 그냥 이클립스에서 새로운 콘솔 위젯/탭을 열어야하거나 당신은 어떤 활성 필터가 있습니까?

그러나 나는 늘이 목적을 위해 Threadpriorities를 사용 IMHO, 특히 같은 짧은 기간에, 실행 순서를 보장하지 않습니다 우선 순위를 설정 http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html

+1

+1 : 운영 체제가 힌트를 무시하지 않더라도, 실행하고자하는 모든 스레드가 자유 CPU를 충분히 갖고 있다면 우선 순위에 관계없이 실행됩니다. –

+2

스레드 우선 순위를 사용하여 추가 할 것입니다 1) 플랫폼 특정 2) 작동하도록 보장되지 및 3) 일부 극단적 인 경우 실행되지 않는 스레드 (기아) 이어질 수 있습니다. – assylias

관련 문제