我尝试通过 android 模拟器和 jvm 运行此代码。在 JVM 上它应该休眠 33-34 毫秒,但在 android 上它是 68-85 毫秒。为什么这么不准确?
谢谢您的帮助。
package android.apps.td;
公共类逻辑实现可运行{
private long frame_time = 1000000000/30;
private long offset = 0;
private long timestamp = 0;
public void run(){
long step;
while(true){
step = System.nanoTime();
if((offset = (step - timestamp)) > frame_time){
System.out.println("FRAME "+(step - timestamp)/1000000+" ms");
timestamp = step;
update();
render();
repaint();
try {
Thread.sleep(frame_time/1000000+1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
try {
System.out.println("Sleep ["+((frame_time-offset)/1000000+1)+" ms]");
Thread.sleep(((frame_time-offset)/1000000+1));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void update() {
}
private void render() {
}
private void repaint() {
}
}