线程优先级问题
import java.awt.*;
import java.util.*;
class Bounce extends Frame {
private Canvas canvas;
public Bounce() {
setTitle("Bounce");
canvas = new Canvas();
add("Center", canvas);
Panel p = new Panel();
p.add(new Button("Start"));
p.add (new Button("Express"));
p.add(new Button("Close"));
add("South", p);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
}
public boolean action(Event evt, Object arg) {
if(arg.equals ("Start")) {
Ball b = new Ball(canvas, Color.BLACK);
b.setPriority (Thread.NORM_PRIORITY); //设置优先级
b.start();
}
else if(arg.equals ("Express")) {
Ball b = new Ball(canvas, Color.YELLOW);
b.setPriority (Thread.MAX_PRIORITY); //设置优先级
b.start ();
}
else if(arg.equals ("Close"))
System.exit(0);
else return super.action(evt, arg);
return true;
}
public static void main(String[] args)
{
Frame f = new Bounce();
f.resize (500, 350);
f.show();
}
}
class Ball extends Thread{ //小球线程
private Canvas box;
private final static int XSIZE = 10;
private final static int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
private Color color;
public Ball(Canvas c, Color co) {
box = c;
color = co;
}
public void draw() {
Graphics g = box.getGraphics ();
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void move() {
Graphics g = box.getGraphics ();
g.setXORMode (box.getBackground ());
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.size ();
if(x < 0) {
x = 0;
dx = -dx;
}
if(x +XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
if(y < 0) {
y = 0;
dy = -dy;
}
if(y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void run() {
draw();
for(; ;) {
move();
try {
Thread.sleep (10);
} catch(InterruptedException e) {
}
}
}
}
在这个程序中,我设置了两个小球并为他们设置了优先级,但两个小球
的运行效果完全相同,为什么?
问题点数:25、回复次数:2Top
1 楼killme2008(为了生态平衡,请保护蛤蟆)回复于 2005-03-30 17:53:40 得分 25
。。优先级只是代表优先级高获得cpu的时间相对多点,它不能保证程序的运行顺序
不要指望通过设置优先级来调配线程Top
2 楼crs_zxf()回复于 2005-03-30 18:51:08 得分 0
但在我把这个程序改成以下这样时却有了运行效果的不同:
import java.awt.*;
import java.util.*;
class Bounce extends Frame {
private Canvas canvas;
public Bounce() {
setTitle("Bounce");
canvas = new Canvas();
add("Center", canvas);
Panel p = new Panel();
p.add(new Button("Start"));
p.add (new Button("Express"));
p.add(new Button("Close"));
add("South", p);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
}
public boolean action(Event evt, Object arg) {
if(arg.equals ("Start")) {
Ball b = new Ball(canvas, Color.BLACK);
b.setPriority (Thread.NORM_PRIORITY);
b.start();
}
else if(arg.equals ("Express")) {
Ball b = new Ball(canvas, Color.YELLOW);
b.setPriority (Thread.MAX_PRIORITY);
b.start ();
}
else if(arg.equals ("Close"))
System.exit(0);
else return super.action(evt, arg);
return true;
}
public static void main(String[] args)
{
Frame f = new Bounce();
f.resize (500, 350);
f.show();
}
}
class Ball extends Thread{
private Canvas box;
private final static int XSIZE = 10;
private final static int YSIZE = 10;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
private Color color;
public Ball(Canvas c, Color co) {
box = c;
color = co;
}
public void draw() {
Graphics g = box.getGraphics ();
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void move() {
Graphics g = box.getGraphics ();
g.setXORMode (box.getBackground ());
g.setColor(color);
g.fillOval (x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.size ();
if(x < 0) {
x = 0;
dx = -dx;
}
if(x +XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
if(y < 0) {
y = 0;
dy = -dy;
}
if(y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval (x, y, XSIZE, YSIZE);
g.dispose ();
}
public void run() {
draw();
for(; ;) {
move();
try {
Thread.sleep (10);
} catch(InterruptedException e) {
}
}
}
}
这又是为什么呢?Top




