1

I have experienced weird behaviour while coding a simple game. Basically, I create a thread with an infinite loop that fires an event in my game every couple of seconds. With the Sysout(runAnimation) in place everything works fine. However as soon as I remove it the even stops occurring.

It appears that the system.out calll is affecting the behavior of the program, does anyone have an idea why this could be happening

Here is the loop:

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();
                    }
                }
            }
    }

It's started during the construction of my class as follows:

animThread = new Thread(new Runnable() {
            @Override
            public void run() {
                GUIView.this.run();
            }
        });
animThread.start();
Maciek
  • 261
  • 2
  • 11

2 Answers2

4

The System.out.println() call is synchronized (most PrintWriter method calls are) so I suspect that there is something that you are not synchronizing on inside of your code. I wonder about the runAnimation field. Could it be that it is set in another thread? Maybe it needs to be volatile? Any field that is modified in one thread and read in another needs to be synchronized or volatile. Reading the Java thread tutorial around synchronization may help.

Without seeing more of the code, it's hard to put a finger on it but I suspect this answer will help anyway.

Lastly, do you really want your thread to spin until runAnimation is true? That's a very bad practice. Maybe you should sleep for some time in the loop if runAnimation is false as well. Another idea is to use a CountDownLatch or other signaling mechanism to pause your thread until it needs to do the animation.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

If your field is not volatile, the JIT can assume it doesn't change and place it in a register or even inline it in code. Placing a system call in this tight loop can

  • prevent the JIT from optimising the code this way as it cannot make assumptions about whether the thread modifies the field.
  • slow down the loop so it is not run 10,000 times (which is the point at which the JIT kicks in)

For more details, here is an article I wrote on the subject Java Memory Model and optimisation.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130