简体   繁体   中英

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.

    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!

I also tried // if ("High Priority" == thrd.getName()) h++; if ("Low Priority" == 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

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 High Priority count 2 Name in thrd is High Priority name in currentName is High Priority

Thrd High Priority count 3 Name in thrd is High Priority name in currentName is High Priority

Thrd Low Priority count 2 Name in thrd is Low Priority name in currentName is Low Priority

Thrd High Priority count 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 Low Priority count 4 Name in thrd is Low Priority

Thrd High Priority count 5 Name in thrd is High Priority name in currentName is High Priority

Thrd High Priority count 6 Name in thrd is High Priority name in currentName is Low Priority

Thrd Low Priority count 5 Name in thrd is Low Priority name in currentName is Low Priority

Thrd Low Priority count 6 Name in thrd is Low Priority name in currentName is Low Priority

Thrd Low Priority count 7 Name in thrd is Low Priority name in currentName is Low Priority

Thrd Low Priority count 8 Name in thrd is Low Priority name in currentName is Low Priority

Thrd Low Priority count 9 Name in thrd is Low Priority name in currentName is Low Priority

Thrd Low Priority count 10 Name in thrd is Low Priority name in currentName is Low Priority

Low Priority terminating. name in currentName is High Priority

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM