public class Test{
public static void main(String args[])throws Exception{
int i=0;
Hello h=new Hello();
Thread t=new Thread(h);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
System.out.println("Please stop saying Hello and say good morning!");
h.stopRunning();
while(i<5){
System.out.println("Good Morning"+i++);
}
}
}
class Hello implements Runnable{
int i=0;
private boolean timeToQuit=false;
public void run(){
while(!timeToQuit){
System.out.println("Hello"+i++);
try{
if(i%2==0) Thread.sleep(10);
}catch(Exception e){}
}
}
public void stopRunning(){
timeToQuit=true;
}
}
我的理解是 :
hello 0
hello 1
Please stop saying Hello and say good morning
Good Morning 0
Good Morning 1
Good Morning 2
Good Morning 3
Good Morning 4
但是程序运行后先执行的是Please stop saying Hello and say good morning
为什么?优先级别我已经设置了。求解。谢谢各位