如何在Java中的一段时间后停止执行? [英] How to stop execution after a certain time in Java?
本文介绍了如何在Java中的一段时间后停止执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在代码中,变量计时器将指定结束while循环的持续时间,例如60秒。
In the code, the variable timer would specify the duration after which to end the while loop, 60 sec for example.
while(timer) {
//run
//terminate after 60 sec
}
推荐答案
long start = System.currentTimeMillis();
long end = start + 60*1000; // 60 seconds * 1000 ms/sec
while (System.currentTimeMillis() < end)
{
// run
}
这篇关于如何在Java中的一段时间后停止执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文