[英]How to wait for (fixed rate) ScheduledFuture to complete on cancellation
是否有內置方法取消已通過ScheduledExecutorService.scheduleAtFixedRate
以固定速率安排的Runnable
任務,如果在調用cancel時恰好正在運行,則等待它完成?
請考慮以下示例:
public static void main(String[] args) throws InterruptedException, ExecutionException {
Runnable fiveSecondTask = new Runnable() {
@Override
public void run() {
System.out.println("5 second task started");
long finishTime = System.currentTimeMillis() + 5_000;
while (System.currentTimeMillis() < finishTime);
System.out.println("5 second task finished");
}
};
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> fut = exec.scheduleAtFixedRate(fiveSecondTask, 0, 1, TimeUnit.SECONDS);
Thread.sleep(1_000);
System.out.print("Cancelling task..");
fut.cancel(true);
System.out.println("done");
System.out.println("isCancelled : " + fut.isCancelled());
System.out.println("isDone : " + fut.isDone());
try {
fut.get();
System.out.println("get : didn't throw exception");
}
catch (CancellationException e) {
System.out.println("get : threw exception");
}
}
該程序的輸出是:
5 second task started
Cancelling task..done
isCancelled : true
isDone : true
get : threw exception
5 second task finished
設置共享易失性標志似乎是最簡單的選擇,但我希望盡可能避免使用它。
java.util.concurrent框架是否內置了此功能?
我不完全確定你想要實現什么,但當我從谷歌搜索到這里時,我認為可能值得回答你的問題。
1)如果你想強行停止繁重的工作量 - 不幸的是它似乎沒有解決方案(當線程沒有響應中斷時)。 處理它的唯一方法是在循環中耗時的操作之間插入Thread.sleep(1)( http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html ) - 也許deamon線程會有幫助,但我真的不鼓勵使用它們。
2)如果你想阻止當前線程直到子線程完成,那么你可以使用get http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html #get()甚至得到超時。
3)如果你想要清除取消子線程,那么你可以調用:
fut.cancel(false);
這不會中斷當前執行,但不會安排它再次運行。
4)如果您的工作負載不重,您只需要等待5秒鍾,然后使用線程睡眠或TimeUnit睡眠。 在這種情況下,中斷/取消將立即生效。
你的例子缺少對Executor的關閉調用導致應用程序不會停止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.