2014-12-30 2 views
0

목표 : 우선 순위가 높은 스레드와 우선 순위가 낮은 스레드가 몇 번 액세스되었는지를 계산하려고합니다. // 다음 코드를 컴파일하면 'h'(int h high)는 0이지만 'l'(low)가 증가합니다. 다음과 같이 실행 코드스레드 우선 순위 계산 - Java

class Priority implements Runnable { 
    int high = 0; 
    int low = 0; 
    int count; Thread thrd; 
    static boolean stop = false; 
    static String currentName; 
    Priority(String name) { 
     thrd = new Thread(this, name); 
     count = 0; 
     currentName = name; 
    } 
    public void run() { 
    System.out.println(thrd.getName() + " starting."); 
    do { 
     count++; 

     if(currentName.compareTo(thrd.getName()) != 0) { 
     currentName = thrd.getName(); 
     System.out.println("In " + currentName); 
     System.out.println("Name in thrd is " + thrd.getName()); 
     System.out.println("name in currentName is " + currentName); 
     if ("High Priority" == currentName) h++; 
     if ("Low Priority" == currentName) l++; 
     } 
     } while(stop == false && count<10); 

    stop = true; 
    System.out.println("\n" + thrd.getName() + " terminating."); 
    } 
    } 

    class PriorityDemo { 
    public static void main(String args[]) { 
     Priority mt1 = new Priority("High Priority"); 
     Priority mt2 = new Priority("Low Priority"); 
     mt1.thrd.setPriority(Thread.NORM_PRIORITY+2); 
     mt2.thrd.setPriority(Thread.NORM_PRIORITY-2); 

     mt1.thrd.start(); 
     mt2.thrd.start(); 

     try { 
     mt1.thrd.join(); 
     mt2.thrd.join(); 
     } catch(InterruptedException e) { 
     System.out.println("Main thread interrupted."); 
     } 

     System.out.println("\n High priority thread counted to " + mt1.count); 
     System.out.println("'n Low priority thread counted to " + mt2.count); 
     System.out.println("In 'mt1' \nhigh is " + mt1.h + " and low is " + mt1.l); 
     System.out.println("In 'mt2' \nhigh is " + mt2.h + " and low is " + mt2.l); 
    } 
    } 

마지막 두 라인은 : MT1에서 높은 인 0 낮다 (예컨대 위해) MT2 높은 985 로우가 985 인 0있다!

나는 또한 시도했다 // if ("High Priority"== thrd.getName()) h ++; if ("낮은 우선 순위"== thrd.getName()) l ++; 하지만 이것도 효과가 없었습니다.

+1

사용 "높은 우선 순위"입니다 .equals (thrd.getName()) – tgkprog

+1

또는 "높은 우선 순위"를 나타내는 열거 형 또는 최종 문자열/int ... – tgkprog

+0

이것은 너무 버그가있는 것처럼 보입니다. if (currentName.compareTo (thrd.getName())! = 0) { currentName = thrd.getName(); 다른 것은 스레드 프로그램은 재미 있지만 대부분의 앱 개발자에게는 쓸모가 없다는 것입니다. j2ee 및 컬렉션에 중점을 둡니다 ...학교와 학업 성적이 – tgkprog

답변

0

도움이 될 수 있습니다. 내가 제대로

package tst; 

public class PriorityDemo { 
    public static void main(String args[]) { 
     Priority mtHigh = new Priority(Priority.HIGH); 
     Priority mtLow = new Priority(Priority.LOW); 
     mtHigh.thrd.setPriority(Thread.NORM_PRIORITY + 3); 
     mtLow.thrd.setPriority(Thread.NORM_PRIORITY - 3); 

     mtHigh.thrd.start(); 
     mtLow.thrd.start(); 

     try { 
      mtHigh.thrd.join(); 
      mtLow.thrd.join(); 
     } catch (InterruptedException e) { 
      System.out.println("Main thread interrupted."); 
     } 

     System.out.println("v 2\n High priority thread counted to " + mtHigh.count); 
     System.out.println("'n Low priority thread counted to " + mtLow.count); 
     System.out.println("In 'mtHigh' \nhigh is " + mtHigh.high + " and low is " + mtHigh.low); 
     System.out.println("In 'mtLow' \nhigh is " + mtLow.high + " and low is " + mtLow.low); 
    } 
} 

class Priority implements Runnable { 
    public final static String HIGH = "High Priority"; 
    public final static String LOW = "Low Priority"; 
    int high = 0; 
    int low = 0; 
    int count; 
    Thread thrd; 
    static boolean stop = false; 
    String currentName;// why static? 

    Priority(String name) { 
     thrd = new Thread(this, name); 
     count = 0; 
     currentName = name; 
    } 

    public void run() { 
     System.out.println(thrd.getName() + " starting."); 
     do { 
      count++; 
      System.out.println("\nThrd " + thrd.getName() + " count " + count); 
      if (thrd.getName().contains(currentName)) { 
       // currentName = thrd.getName(); 
       //System.out.println("In " + currentName); 
       System.out.println("Name in thrd is " + thrd.getName()); 
       System.out.println("name in currentName is " + currentName); 
       if (currentName.contains(HIGH)) 
        high++; 
       if (currentName.contains(LOW)) 
        low++; 
      } 
     } while (stop == false && count < 10); 

     stop = true;//this static so high runs faster and stops low before its count is 10 
     System.out.println("\n" + thrd.getName() + " terminating."); 
    } 
} 

출력 목표를 가정하면 내가 가지고 :

낮은 우선 순위의 시작을. 높은 우선 순위 시작.

THRD에서

THRD 낮은 우선 순위 카운트 1

THRD 높은 우선 순위의 수를 1 명 THRD 에서 높은 우선 순위의 이름 currentName에서 낮은 우선 순위의 이름 currentName에서 낮은 우선 순위의 이름은 높은 우선 순위

THRD 높은입니다입니다 우선 수 THRD 2 명 currentName에서 높은 우선 순위의 이름은 높은 우선 순위

입니다 THRD에서

THRD 높은 우선 순위의 수를 3 명 currentName에서 높은 우선 순위의 이름은 높은 우선 순위입니다 THRD에서 12,992,967,가

THRD 낮은 우선 순위의 수를 2 명 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

입니다 THRD에서

THRD 높은 우선 순위 카운트 4

THRD 낮은 우선 순위의 수를 3 명 currentName에 낮은 우선 순위의 이름입니다 THRD에서 낮은 우선 순위의 이름 currentName에서 높은 우선 순위의 이름은 높은 우선 순위입니다입니다

THRD에서

THRD 낮은 우선 순위의 수를 4 명 낮은 우선 순위

입니다 THRD에서

THRD 높은 우선 순위의 수를 5 명 currentName에서 높은 우선 순위의 이름은 높은 우선 순위

입니다 THRD에서

THRD 높은 우선 순위의 수를 6 명 currentName에서 높은 우선 순위의 이름은 낮은 우선 순위

THRD 낮은 우선 순위 수 5입니다 THRD의 이름은 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

입니다 THRD에서

THRD 낮은 우선 순위의 수를 6 명 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

입니다 THRD에서THRD 낮은 우선 순위의 수를 7 명 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

입니다 THRD에서

THRD 낮은 우선 순위 수 8 명 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

THRD 낮은 우선 순위 카운트 9입니다 THRD의 이름은 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

THRD에서

THRD 낮은 우선 순위의 수를 10 명 currentName에서 낮은 우선 순위의 이름은 낮은 우선 순위

입니다

낮은 프리 오입니다 종결. currentName의 이름은 높은 우선 순위입니다.

높은 우선 순위 종료 중입니다. 2 절 우선 순위가 높은 스레드가 mtHigh 6 '에서 10로 계산 N 낮은 우선 순위의 스레드'로 계산 '이 높은 6과 낮은 0 이다'고 mtLow '가 0과 낮은 10

+0

그는 또한 동기화와 함께 갈 수 있습니다. Synchronize block 또는 method를 만들 수 있습니다. – Prashant

+0

동기화 할 필요가 없습니다. 두 개의 변수가 스레드간에 액세스되지 않습니다. 그런데 정지를 제외하고, 그것의 거의 문제는 단지 1 개의 장소에서 사실이게되고, 다른 실에서 조사했다. 우선 순위가 낮은 스레드가 추가 반복을 위해 실행된다는 점을 제외하고는 학술 프로젝트에 부작용이 있을지 의심됩니다. – tgkprog

+0

tgkprog 다시 한 번 감사드립니다! 그리고 또한 Prashant에게 당신의 제안에 대해 : D –