java 线程sleep 疑问
为什么把Thread.sleep(500);放在if(!stop)语句块内会造成死循环,cpu爆满呢?而把Thread.sleep(500)放在while(true)语句块内就
正常了呢?
class Twinkle extends Thread{
boolean stop=true;
public void run(){
int which=0;
while(true){
if(!stop){
if(which==0){
which=1;
//trayIcon.setImage(ImageAndUIFactory.trayYellow);
}
else{
which=0;
//trayIcon.setImage(ImageAndUIFactory.trayWhite);
}
}
else{
//trayIcon.setImage(ImageAndUIFactory.trayYellow);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}