Thread.sleep Issues in Web Browser
Basically, I have written a JApplet and I now need to set a constant frame rate. I implemented a very simple segment of code that calls Thread.sleep:
long sleep = (long) (20e6 - System.nanoTime() + t);
if(sleep > 0){
try{
Thread.sleep((long) (sleep / 1e6));
} catch (Exception e) {}
}
t = System.nanoTime();
and when I run in Eclipse, all is good, but when I run it in a browser, the frame rate becomes extremely unstable and it looks like it's lagging. In the browser, it runs fine without the Thread.sleep call, just way faster than I need it to.开发者_StackOverflow
Any help on why this is or ways to get around this problem would be greatly appreciated.
As an alternative, consider javax.swing.Timer
, as shown here.
精彩评论