簡體   English   中英

計算線程優先級-Java

[英]Counting thread priority - Java

目標:想統計訪問高優先級和低優先級線程的次數。 //當我編譯以下代碼時,“ h”(int h代表高)保持為零,但“ l”(低)卻增加。

    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是低(為例如)985高MT2是0低是985!

我也嘗試過// // if(“ High Priority” == thrd.getName())h ++; if(“低優先級” == thrd.getName())l ++; 但這也沒有解決。

這可能會有所幫助。 如果我正確地設定了您的目標

    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.");
    }
}

我得到的輸出:

低優先級啟動。 高優先級啟動。

低優先級計數1

Thrd高優先級計數1 thrd中的名稱為高優先級thrd中的名稱為低優先級currentName中的低優先級名稱為currentName中的低優先級名稱為High Priority

Thrd高優先級計數2 thrd中的名稱為高優先級,currentName中的名稱為高優先級

Thrd高優先級計數3 thrd中的名稱為高優先級,currentName中的名稱為高優先級

Thrd低優先級計數2 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd高優先級計數4

Thrd低優先級計數3 thrd中的名稱為currentName中的低優先級名稱為低優先級thrd中的名稱為currentName中的高優先級名稱為currentPrince

Thrd低優先級計數4 thrd中的名稱為低優先級

Thrd高優先級計數5 thrd中的名稱為currentName中的高優先級名稱為High Priority

Thrd高優先級計數6 thrd中的名稱為currentName中的高優先級名稱為Low Priority

Thrd低優先級計數5 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd低優先級計數6 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd低優先級計數7 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd低優先級計數8 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd低優先級計數9 thrd中的名稱為低優先級,currentName中的名稱為低優先級

Thrd低優先級計數10 thrd中的名稱為低優先級,currentName中的名稱為低優先級

低優先級終止。 currentName中的名稱為“高優先級”

高優先級終止。 v 2高優先級線程計數為6'n低優先級線程計數為10在'mtHigh'中高為6而低為0在'mtLow'中高為0而低為10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM