Timers and JOGL GLCanvas.display() performance

To run the display of a GLCanvas I used a Timer to run at about a rate of 60 frames per second which is about a 16 millisecond delay.
I created a custom class that extends Timer:

public class GameTimer extends Timer{...

and a class that extends TimerTask:

private class GameTimerTask extends TimerTask{...

which has a run method:

public void run(){...

which calls another function which at the moment does nothing except for calling GLCanvas.display() which at the moment also does nothing, except for drawing a single square in the screen.

then:

scheduleAtFixedRate(new GameTimerTask(), 0, 16);

which actually starts the timer.

It worked but then I noticed that running it chews up an entire CPU and very very slowly increases the memory usage.

So now I switched to using a thread and a loop with a sleep:


public class GameTimer extends Thread{....

//The run method
public void run(){
  while(run){
   canvas.display();
   try{
     Thread.sleep(16);
   }
   catch(InterruptedException e){
     return;
   }
 }
}

It works fine and running at about 60 fps it on has 2% CPU usage and the memory usage stays at about the same amount.

They both do exactly the same but the timer just hogs the CPU, am I doing something wrong with the timer that’s causing this? or is the Timer just not that fast?

I did leave out some code to keep it as short as possible, but the basics are there.

Why not using the “Animator” or “FPSAnimator” classes in the “com.sun.opengl.util” package?

I guess 99% of the people out there are relying on them and they should be optimal in term of performance.

Once you choose the one that fits (“Animator” works well if you need to be synched with the screen’s refresh-rate, while “FPSAnimator” allows for more control…), it’s pretty easy to create a new animator-class and to “start”. From this moment: your canvas’ display() method will be invoked automatically at each frame…

Because I wanted to do it myself, I don’t want to just use the Animator class without knowing what it’s doing.

It is open source, you can watch the source code of Animator and FPSAnimator if you want. However, the class Animator doesn’t work in my game, it gives strange results, big slowdowns and problems of sound, therefore I don’t use it.

That’s a good idea.
Where would I find the source?

Go to the JOGL website, choose the release you want and download jogl-1.1.1-src.zip.