This is the run method from my game loop thread (a modified version of another SO answer). The running boolean is basically there as a way to pause and unpause the game, and it works just fine normally. I tried to add a constructor that lets the game start out as paused, or running == false and it didn't work. The game would never start, despite successfully setting running to true. I added some print statements to figure out what was going on. Then a weird thing happened. The addition of the print statement fixed the problem.
private void reset() {
initialTime = System.nanoTime();
deltaU = 0; deltaF = 0;
frames = 0; ticks = 0;
timer = System.currentTimeMillis();
}
public void run() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
reset();
boolean wasRunning=false;
while (true) {
System.out.println("in loop");
if (running) {
System.out.println("running");
if(!wasRunning){
wasRunning=true;
reset();
}
long currentTime = System.nanoTime();
deltaU += (currentTime - initialTime) / timeU;
deltaF += (currentTime - initialTime) / timeF;
initialTime = currentTime;
if (deltaU >= 1) {
update();
ticks++;
deltaU--;
}
if (deltaF >= 1) {
render();
frames++;
deltaF--;
}
if (System.currentTimeMillis() - timer > 1000) { //prints warning if dropped frames
if (printTime) {
System.out.println(String.format("UPS: %s, FPS: %s", ticks, frames));
}
if(ticks!=60 || frames!=60)
System.out.println(String.format("UPS: %s, FPS: %s", ticks, frames));
frames = 0;
ticks = 0;
timer += 1000;
}
}
else
wasRunning=false;
}
}
the output is
in loop
running
in loop
running
in loop
running...
And the game runs with a lot of studder (though the fps warning is oddly silent). when I comment out the System.out.println("in loop"), there is no output, and setting running to true does nothing.
So the fact that there is a print statement is actually causing the loop to execute, but omitting it causes it to fail.
Note that if running is set to true at first, the print statements are removed, the pause and unpause functionality works as expected.
I also tried renaming running to running2 just to make sure it wasn't overriding anything.