目标:想要计算访问高优先级和低优先级线程的次数。 //当我编译下面的代码时,' h'(int h代表高)保持为零,但是'(低)增加。
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(&#34; High Priority&#34; == thrd.getName())h ++; if(&#34; Low Priority&#34; == thrd.getName())l ++; 但这也没有成功。
答案 0 :(得分: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 Low Priority count 1
Thrd High Priority count 1 thrd中的名称是thrd中的高优先级名称 是currentName中的低优先级名称是低优先级名称 currentName是高优先级
Thrd High Priority count 2 thrd中的名称是高优先级名称 currentName是高优先级
Thrd High Priority count 3 thrd中的名称是高优先级名称 currentName是高优先级
Thrd Low Priority count 2 thrd中的名称是低优先级名称 currentName是低优先级
Thrd High Priority count 4
Thrd Low Priority count 3 thrd中的名称是低优先级名称 currentName是低优先级名称,thrd是高优先级名称 currentName是高优先级
Thrd Low Priority count 4 thrd中的名称为低优先级
Thrd High Priority count 5 thrd中的名称是高优先级名称 currentName是高优先级
Thrd High Priority count 6 thrd中的名称是高优先级名称 currentName是低优先级
Thrd Low Priority count 5 thrd中的名称是低优先级名称 currentName是低优先级
Thrd Low Priority count 6 thrd中的名称是低优先级名称 currentName是低优先级
Thrd Low Priority count 7 thrd中的名称是低优先级名称 currentName是低优先级
Thrd Low Priority count 8 thrd中的名称是低优先级名称 currentName是低优先级
Thrd Low Priority count 9 thrd中的名称是低优先级名称 currentName是低优先级
Thrd Low Priority count 10 thrd中的名称是低优先级名称 currentName是低优先级
低优先级终止。 currentName中的名称是高优先级
高优先级终止。 v 2高优先级线程计为6&n; n 低优先级线程计为10 In&#39; mtHigh&#39;高是6而低是0 在&#39; mtLow&#39; high为0而low为10