0
我有一个特定的任务必须定期执行,或者只是基于一个条件执行一次。我使用以下方法:根据条件定期或只运行一次ScheduledExecutorService
Runnable r = new Runnable() {
public void run()
{
//Execute task
}
};
final long freq = frequencyOfTask;
ScheduledExecutorService dataTimer = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> dataTimerHandle = dataTimer.scheduleAtFixedRate(r, 0L, freq, TimeUnit.MILLISECONDS);
if(!isDynamic)
{
dataTimerHandle.cancel(false); //cancel the event in main thread while asking the already submitted tasks to complete.
}
任务运行正常的情况下isDynamic
是false
,即,其中任务没有取消。但是,对于另一种情况(当执行需要一次)它根本不执行。
其他情况你在说什么? – 2015-03-19 05:46:47
另一种情况是只需执行一次执行 – abksrv 2015-03-19 05:48:19