我在编写一个简单的游戏时遇到了奇怪的行为。基本上,我创建了一个无限循环的线程,每隔几秒钟在我的游戏中触发一个事件。使用 Sysout(runAnimation) 一切正常。然而,一旦我删除它,甚至停止发生。
似乎 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();