0

I create a project in java which is a typing game. I use java.util.Timer.schedule to set a Timer to repaint my window. Code here:

timer.schedule(new TimerTask() {
    @Override
    public void run() {
        // Some other codes ...
        repaint();
    }
}, 0, 1000 / HZ);

HZ is 200, the code work well on Ubuntu but when I run it on Windows, it's slowly obviously than on Ubuntu, I am confused about that.

lllwwwbbb
  • 63
  • 1
  • 4

1 Answers1

0

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

Evingle
  • 49
  • 4
  • Thank you sincerely that you make me understand it. I use method `Timer.scheduleAtFixedRate` instead of `Timer.schedule` and my project run well on Windows too. – lllwwwbbb Oct 08 '16 at 14:50
  • I forgot to mention Timer.scheduleAtFixedRate. Anyway, you found it. Glad it helped – Evingle Oct 08 '16 at 15:30