我需要将 while 循环暂停特定的毫秒数。我尝试过使用 Thread.sleep(duration) 但它不准确,特别是在循环场景中。毫秒精度在我的程序中很重要。
这是我不想返回检查条件直到expectedElapsedTime
已经过去了。
while (condition) {
time = System.currentTimeMillis();
//do something
if (elapsedTime(time) < expectedElapsedTime) ) {
pause the loop // NEED SUBSTITUTE FOR Thread.sleep()
}
// Alternative that I have tried but not giving good results is
while ((elapsedTime(time) < expectedElapsedTime)) {
//do nothing
}
}
long elapsedTime(long time) {
long diff = System.currentTimeMillis() - time;
return diff;
}
Try a 调度线程池执行器 http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html。它应该提供更可靠的计时结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)