简体   繁体   English

计算线程优先级-Java

[英]Counting thread priority - Java

Goal: wanted to count how many number of times High Priority and Low Priority threads were accessed. 目标:想统计访问高优先级和低优先级线程的次数。 //When I compile the following code, the 'h'(int h standing for high) remains zero but the 'l'(low) increases. //当我编译以下代码时,“ 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);
     }
    }

The last two lines of the executed code are as follows: high in mt1 is 0 low is (for eg)985 high in mt2 is 0 low is 985! 是所执行代码的最后两行如下:在高MT1为0是低(为例如)985高MT2是0低是985!

I also tried // if ("High Priority" == thrd.getName()) h++; 我也尝试过// // if(“ High Priority” == thrd.getName())h ++; if ("Low Priority" == thrd.getName()) l++; if(“低优先级” == thrd.getName())l ++; But this too didn't work out. 但这也没有解决。

This might help. 这可能会有所帮助。 if I assumed your goals correctly 如果我正确地设定了您的目标

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

Output I got : 我得到的输出:

Low Priority starting. 低优先级启动。 High Priority starting. 高优先级启动。

Thrd Low Priority count 1 低优先级计数1

Thrd High Priority count 1 Name in thrd is High Priority Name in thrd is Low Priority name in currentName is Low Priority name in currentName is High Priority Thrd高优先级计数1 thrd中的名称为高优先级thrd中的名称为低优先级currentName中的低优先级名称为currentName中的低优先级名称为High Priority

Thrd High Priority count 2 Name in thrd is High Priority name in currentName is High Priority Thrd高优先级计数2 thrd中的名称为高优先级,currentName中的名称为高优先级

Thrd High Priority count 3 Name in thrd is High Priority name in currentName is High Priority Thrd高优先级计数3 thrd中的名称为高优先级,currentName中的名称为高优先级

Thrd Low Priority count 2 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数2 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd High Priority count 4 Thrd高优先级计数4

Thrd Low Priority count 3 Name in thrd is Low Priority name in currentName is Low Priority Name in thrd is High Priority name in currentName is High Priority Thrd低优先级计数3 thrd中的名称为currentName中的低优先级名称为低优先级thrd中的名称为currentName中的高优先级名称为currentPrince

Thrd Low Priority count 4 Name in thrd is Low Priority Thrd低优先级计数4 thrd中的名称为低优先级

Thrd High Priority count 5 Name in thrd is High Priority name in currentName is High Priority Thrd高优先级计数5 thrd中的名称为currentName中的高优先级名称为High Priority

Thrd High Priority count 6 Name in thrd is High Priority name in currentName is Low Priority Thrd高优先级计数6 thrd中的名称为currentName中的高优先级名称为Low Priority

Thrd Low Priority count 5 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数5 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd Low Priority count 6 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数6 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd Low Priority count 7 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数7 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd Low Priority count 8 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数8 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd Low Priority count 9 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数9 thrd中的名称为低优先级,currentName中的名称为低优先级

Thrd Low Priority count 10 Name in thrd is Low Priority name in currentName is Low Priority Thrd低优先级计数10 thrd中的名称为低优先级,currentName中的名称为低优先级

Low Priority terminating. 低优先级终止。 name in currentName is High Priority currentName中的名称为“高优先级”

High Priority terminating. 高优先级终止。 v 2 High priority thread counted to 6 'n Low priority thread counted to 10 In 'mtHigh' high is 6 and low is 0 In 'mtLow' high is 0 and low is 10 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