constant FPS

Can somebody tell me if that is normal?
If I execute the following code


import javax.swing.*;


public class TimerTest {

	public static void main (String args[]) {
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new TimerTest();
			}
		});
	}

	public TimerTest(){
		long startTime = System.currentTimeMillis();
		System.out.println(startTime);

		try{
			Thread.sleep(20);
		}
		catch(Exception e){}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime);

		System.out.println("difference= "+(endTime-startTime));
	}
}

I get something like:
1190718128803
1190718128827
difference= 24

So the Thread is sleeping for 24ms (always more or less between 23 and 27). I have a dual core processor and I’ve heard that nanoTimer gives difficulties with dual-core.

You could try moving the System.out.println(startTime) to after the endTime = statement, as the way you have it coded you are measuring the print time as well as the sleep time.

System.currentTimeMillis() doesn’t have a good granularity on Windows, so use System.nanoTime(). The dual-core issue actually depends on where the OS gets it’s timing info - see here: http://support.microsoft.com/kb/895980

Also, the sleep() function isn’t too accurate on Windows, but see a workaround here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6435126

Thanks for the answers,
but still…
I’m using Linux, and if I use nanoTimer(), I have the same result .