计算线程优先级-Java

计算线程优先级-Java,java,thread-priority,Java,Thread Priority,目标:要计算访问高优先级和低优先级线程的次数。 //当我编译下面的代码时,“h”(inth代表高)保持为零,“l”(低)增加 class Priority implements Runnable { int high = 0; int low = 0; int count; Thread thrd; static boolean stop = false; static String currentName; Priority(Stri

目标:要计算访问高优先级和低优先级线程的次数。 //当我编译下面的代码时,“h”(inth代表高)保持为零,“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);
     }
    }
类优先级实现可运行{
int高=0;
int低=0;
整数计数;线程thrd;
静态布尔停止=false;
静态字符串currentName;
优先级(字符串名称){
thrd=新线程(此,名称);
计数=0;
currentName=名称;
}
公开募捐{
System.out.println(thrd.getName()+“start.”);
做{
计数++;
if(currentName.compareTo(thrd.getName())!=0){
currentName=thrd.getName();
System.out.println(“In”+当前名称);
System.out.println(“thrd中的名称是”+thrd.getName());
System.out.println(“currentName中的名称为”+currentName);
if(“高优先级”==currentName)h++;
if(“低优先级”==currentName)l++;
}

}while(stop==false&&count这可能会有帮助。如果我正确地假设了你的目标

    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.");
    }
}
package-tst;
公开课优先演示{
公共静态void main(字符串参数[]){
优先级mtHigh=新优先级(Priority.HIGH);
优先级mtLow=新优先级(Priority.LOW);
mtHigh.thrd.setPriority(Thread.NORM_PRIORITY+3);
mtLow.thrd.setPriority(Thread.NORM_PRIORITY-3);
mtHigh.thrd.start();
mtLow.thrd.start();
试一试{
mtHigh.thrd.join();
mtLow.thrd.join();
}捕捉(中断异常e){
System.out.println(“主线程中断”);
}
System.out.println(“v2\n高优先级线程计数为”+mtHigh.count);
System.out.println(“'n低优先级线程计数为“+mtLow.count”);
System.out.println(“在'mtHigh'中\n高是“+mtHigh.high+”,低是“+mtHigh.low”);
System.out.println(“在'mtLow'中\n高是“+mtLow.high+”,低是“+mtLow.low”);
}
}
类优先级实现可运行{
公共最终静态字符串HIGH=“高优先级”;
公共最终静态字符串LOW=“低优先级”;
int高=0;
int低=0;
整数计数;
螺纹螺纹;
静态布尔停止=false;
String currentName;//为什么是静态的?
优先级(字符串名称){
thrd=新线程(此,名称);
计数=0;
currentName=名称;
}
公开募捐{
System.out.println(thrd.getName()+“start.”);
做{
计数++;
System.out.println(“\nThrd”+thrd.getName()+“count”+count”);
if(thrd.getName().contains(currentName)){
//currentName=thrd.getName();
//System.out.println(“In”+当前名称);
System.out.println(“thrd中的名称是”+thrd.getName());
System.out.println(“currentName中的名称为”+currentName);
if(currentName.contains(高))
高++;
if(currentName.contains(低))
低++;
}
}while(stop==false&&count<10);
stop=true;//此static so high在其计数为10之前运行得更快,并停止在低位
System.out.println(“\n”+thrd.getName()+”终止“);
}
}
我得到的输出:

低优先级启动。高优先级启动

Thrd低优先级计数1

Thrd中的Thrd高优先级计数1名称是Thrd中的高优先级名称 currentName中的是低优先级名称currentName中的是低优先级名称 currentName具有高优先级

Thrd中的Thrd高优先级计数2名称是Thrd中的高优先级名称 currentName具有高优先级

Thrd中的Thrd高优先级计数3名称是Thrd中的高优先级名称 currentName具有高优先级

Thrd中的Thrd低优先级计数2名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd高优先级计数4

Thrd中的Thrd低优先级计数3名称是Thrd中的低优先级名称 currentName在thrd中是低优先级名称在thrd中是高优先级名称 currentName具有高优先级

Thrd低优先级计数4 Thrd中的名称为低优先级

Thrd中的Thrd高优先级计数5名称是Thrd中的高优先级名称 currentName具有高优先级

Thrd中的Thrd高优先级计数6名称是Thrd中的高优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数5名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数6名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数7名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数8名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数9名称是Thrd中的低优先级名称 currentName的优先级较低

Thrd中的Thrd低优先级计数10名称是Thrd中的低优先级名称 currentName的优先级较低

低优先级终止。currentName中的名称为高优先级

高优先级终止。v 2高优先级线程计数为6'n “mtHigh”中计数为10的低优先级线程高为6,低为0 在“mtLow”中,高值为0,低值为10


使用“High Priority”.equals(thrd.getName())或更好的枚举或最终字符串/int来表示“High Priority”…这似乎也有问题。如果(currentName.compareTo(thrd.getName())!=0{currentName=thrd.getName();另一件事是线程程序很有趣,但对大多数应用程序开发人员来说是无用的。关注j2ee和集合…除非你在学校,这是academicuse'equals()'方法而不是您的条件中的'=='运算符。您期望的是什么,为什么?他也可以进行同步。他可以创建同步块或方法。不需要同步。没有两个可变变量可以跨线程访问。我们