优先级
线程的优先级(priority)告诉调试程序该线程的重要程度有多大。如果有大量线程都被堵塞,都在等候运行,调试程序会首先运行具有最高优先级的那个线程。然而,这并不表示优先级较低的线程不会运行(换言之,不会因为存在优先级而导致死锁)。若线程的优先级较低,只不过表示它被准许运行的机会小一些而已。
可用getpriority()方法读取一个线程的优先级,并用setpriority()改变它。在下面这个程序片中,大家会发现计数器的计数速度慢了下来,因为它们关联的线程分配了较低的优先级:
//: counter5.java
// adjusting the priorities of threads
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class ticker2 extends thread {
private button
b = new button("toggle"),
incpriority = new button("up"),
decpriority = new button("down");
private textfield
t = new textfield(10),
pr = new textfield(3); // display priority
private int count = 0;
private boolean runflag = true;
public ticker2(container c) {
b.addactionlistener(new togglel());
incpriority.addactionlistener(new upl()); decpriority.addactionlistener(new downl());
panel p = new panel();
p.add(t);
p.add(pr);
p.add(b);
p.add(incpriority);
p.add(decpriority);
c.add(p);
}
class togglel implements actionlistener {
public void actionperformed(actionevent e) {
runflag = !runflag;
}
}
class upl implements actionlistener {
public void actionperformed(actionevent e) {
int newpriority = getpriority() + 1;
if(newpriority > thread.max_priority)
newpriority = thread.max_priority;
setpriority(newpriority);
}
}
class downl implements actionlistener {
public void actionperformed(actionevent e) {
int newpriority = getpriority() - 1;
if(newpriority < thread.min_priority)
newpriority = thread.min_priority;
setpriority(newpriority);
}
}
public void run() {
while (true) {
if(runflag) {
t.settext(integer.tostring(count++));
pr.settext(
integer.tostring(getpriority()));
}
yield();
}
}
}
public class counter5 extends applet {
private button
start = new button("start"),
upmax = new button("inc max priority"),
downmax = new button("dec max priority");
private boolean started = false;
private static final int size = 10;
private ticker2[] s = new ticker2[size];
private textfield mp = new textfield(3);
public void init() {
for(int i = 0; i < s.length; i++)
s[i] = new ticker2(this);
add(new label("max_priority = "
+ thread.max_priority));
add(new label("min_priority = "
+ thread.min_priority));
add(new label("group max priority = "));
add(mp);
add(start);
add(upmax); add(downmax);
start.addactionlistener(new startl());
upmax.addactionlistener(new upmaxl());
downmax.addactionlistener(new downmaxl());
showmaxpriority();
// recursively display parent thread groups:
threadgroup parent =
s[0].getthreadgroup().getparent();
while(parent != null) {
add(new label(
"parent threadgroup max priority = "
+ parent.getmaxpriority()));
parent = parent.getparent();
}
}
public void showmaxpriority() {
mp.settext(integer.tostring(
s[0].getthreadgroup().getmaxpriority()));
}
class startl implements actionlistener {
public void actionperformed(actionevent e) {
if(!started) {
started = true;
for(int i = 0; i < s.length; i++)
s[i].start();
}
}
}
class upmaxl implements actionlistener {
public void actionperformed(actionevent e) {
int maxp =
s[0].getthreadgroup().getmaxpriority();
if(++maxp > thread.max_priority)
maxp = thread.max_priority;
s[0].getthreadgroup().setmaxpriority(maxp);
showmaxpriority();
}
}
class downmaxl implements actionlistener {
public void actionperformed(actionevent e) {
int maxp =
s[0].getthreadgroup().getmaxpriority();
if(--maxp < thread.min_priority)
maxp = thread.min_priority;
s[0].getthreadgroup().setmaxpriority(maxp);
showmaxpriority();
}
}
public static void main(string[] args) {
counter5 applet = new counter5();
frame aframe = new frame("counter5");
aframe.addwindowlistener(
new windowadapter() {
public void windowclosing(windowevent e) {
system.exit(0);
}
});
aframe.add(applet, borderlayout.center);
aframe.setsize(300, 600);
applet.init();
applet.start();
aframe.setvisible(true);
}
} ///:~
ticker采用本章前面构造好的形式,但有一个额外的textfield(文本字段),用于显示线程的优先级;以及两个额外的按钮,用于人为提高及降低优先级。
也要注意yield()的用法,它将控制权自动返回给调试程序(机制)。若不进行这样的处理,多线程机制仍会工作,但我们会发现它的运行速度慢了下来(试试删去对yield()的调用)。亦可调用sleep(),但假若那样做,计数频率就会改由sleep()的持续时间控制,而不是优先级。
counter5中的init()创建了由10个ticker2构成的一个数组;它们的按钮以及输入字段(文本字段)由ticker2构建器置入窗体。counter5增加了新的按钮,用于启动一切,以及用于提高和降低线程组的最大优先级。除此以外,还有一些标签用于显示一个线程可以采用的最大及最小优先级;以及一个特殊的文本字段,用于显示线程组的最大优先级(在下一节里,我们将全面讨论线程组的问题)。最后,父线程组的优先级也作为标签显示出来。
按下“up”(上)或“down”(下)按钮的时候,会先取得ticker2当前的优先级,然后相应地提高或者降低。
运行该程序时,我们可注意到几件事情。首先,线程组的默认优先级是5。即使在启动线程之前(或者在创建线程之前,这要求对代码进行适当的修改)将最大优先级降到5以下,每个线程都会有一个5的默认优先级。
最简单的测试是获取一个计数器,将它的优先级降低至1,此时应观察到它的计数频率显著放慢。现在试着再次提高优先级,可以升高回线程组的优先级,但不能再高了。现在将线程组的优先级降低两次。线程的优先级不会改变,但假若试图提高或者降低它,就会发现这个优先级自动变成线程组的优先级。此外,新线程仍然具有一个默认优先级,即使它比组的优先级还要高(换句话说,不要指望利用组优先级来防止新线程拥有比现有的更高的优先级)。
最后,试着提高组的最大优先级。可以发现,这样做是没有效果的。我们只能减少线程组的最大优先级,而不能增大它。
闽公网安备 35060202000074号