【发布时间】:2015-03-19 05:37:09
【问题描述】:
我有一个特定的任务必须定期执行,或者根据条件只执行一次。我正在使用以下方法:
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 的情况下,任务运行良好,即任务未被取消。但是,对于另一种情况(当需要执行一次)它根本不执行。
【问题讨论】:
-
你说的是其他哪种情况??
-
另一种情况是只需要执行一次
标签: java concurrency timer scheduling java.util.concurrent