【发布时间】:2010-07-10 15:52:40
【问题描述】:
我知道这个问题here,但我有一个稍微不同的问题。如果我希望自己通过各种 Thread 方法(而不是通过实用程序类或 Quartz)在特定时间运行 Thread 手动编码,那么最有效(就开销而言) ) 对此进行编码。
我考虑过:
boolean wasInterrupted = false;
while (System.currentTimeMillis() < executionTimeInMillis) {
try {
Thread.sleep(X);
} catch (InterruptedException ie) {
wasInterrupted = true;
}
}
if (!wasInterrupted) {
doMyThing();
}
有没有更好的方法?这是原始和幼稚吗?
【问题讨论】:
-
我假设您不想使用调度执行器服务?无论如何,使用 System.nanoTime stackoverflow.com/questions/510462/… 可能效率更高,当然您每次循环都需要/想要重新计算 X
标签: java scheduling