我在编写简单的游戏时遇到了奇怪的行为。基本上,我创建了一个无限循环的线程,每隔几秒就会在游戏中触发一个事件。随着Sysout(runAnimation)到位,一切正常。然而,只要我删除它甚至停止发生。Heisenbug:线程不运行没有sysout
看来,System.out的calll是影响程序的行为,没有任何人有一个想法,为什么这可能发生
这里是循环:
private void run(){
long lastTime = -1;
while(true){
int timePerStep = (int) Math.ceil(ANIM_TIME/frequency);
System.out.println(runAnimation);
if(runAnimation && System.currentTimeMillis() - lastTime > timePerStep){
lastTime = System.currentTimeMillis();
controller.step();
try {
Thread.sleep(timePerStep);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
它在开始我班的建设如下:
animThread = new Thread(new Runnable() {
@Override
public void run() {
GUIView.this.run();
}
});
animThread.start();