Jan Burak
Jan Burak

Reputation: 25

Java: wait() limits the fps to 64

I have this in my code in the main loop(2D game in a window):

try{
  synchronized(this){

    wait(3);
  }
}
catch(Exception ex) {
  System.out.println(ex);
}

This piece of code causes to FPS to cap at 64 and I don't know why. I don't use any other synchronized blocks. Amusingly, when the web browser is open, the fps is no longer capped. Could anybody tell me how to get rid of that 64 fps limit? I didn't manage to find any other topics with this problem.

EDIT:

How could the browser change fps?

Upvotes: 1

Views: 365

Answers (3)

Michał Kosmulski
Michał Kosmulski

Reputation: 10020

If you don't use any other synchronized blocks, then there is no one to notify() your thread. This means you are probably telling your app to sleep for at least 3 milliseconds on each iteration. Additionally, you may lose some more time because of the thread giving up its time quantum as it goes to sleep and the clock resolution is also usually more than 1 ms, depending on OS.

64 FPS means a frame takes slightly above 15 ms. Tell us what your "uncapped" FPS is, calculate to how many ms per frame it translates and see what the difference is. If the difference in frame time wit hand without the losted code is on the order of 3-10 ms (10 ms is probably a reasonable upper limit on clock granularity on a sane system), it is probably only the result of the wait(). If without the wait() your frames only take 1 ms, there is probably some additional effect.

EDIT after Jan's comment: 115 FPS means 8.7 ms per frame. Going from that to 15 because of wait(3) seems probable. I'm not sure how running another app in the background may influence it. Perhaps having another task in the background influences the scheduler's behavior. Does the other task bring FPS back to 115 or to some intermediate value?

EDIT after Jan's second comment: if it's 180 instead of 115, we have 5.5 ms per frame. This increases the difference, but with the Windows clock being rather coarse (as others pointed out), this is still within the limits of the effect described above.

Upvotes: 2

OldCurmudgeon
OldCurmudgeon

Reputation: 65889

Although the resolution of the parameters of wait and sleep are in milliseconds you will almost certainly never get exactly the delay you are requesting.

On a Windows system the resolution is approximately 15ms giving 1000/15 = 66fps

Upvotes: 5

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

Don't try to get more than 60 fps. A modern Display won't show more than 60fps.

Upvotes: 0

Related Questions