开发者

Thread.sleep Issues in Web Browser

开发者 https://www.devze.com 2023-04-03 05:17 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消