日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用ExecutorService来停止线程服务

發布時間:2024/2/28 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用ExecutorService来停止线程服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 使用shutdown
    • 使用shutdownNow

使用ExecutorService來停止線程服務

之前的文章中我們提到了ExecutorService可以使用shutdown和shutdownNow來關閉。

這兩種關閉的區別在于各自的安全性和響應性。shutdownNow強行關閉速度更快,但是風險也更大,因為任務可能正在執行的過程中被結束了。而shutdown正常關閉雖然速度比較慢,但是卻更安全,因為它一直等到隊列中的所有任務都執行完畢之后才關閉。

使用shutdown

我們先看一個使用shutdown的例子:

public void useShutdown() throws InterruptedException {ExecutorService executor = Executors.newFixedThreadPool(10);Runnable runnableTask = () -> {try {TimeUnit.MILLISECONDS.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}};executor.submit(runnableTask);executor.shutdown();executor.awaitTermination(800, TimeUnit.MILLISECONDS);}

awaitTermination將會阻塞直到所有正在執行的任務完成,或者達到指定的timeout時間。

使用shutdownNow

當通過shutdownNow來強行關閉ExecutorService是, 它會嘗試取消正在執行的任務,并返回所有已經提交但是還沒有開始的任務。從而可以將這些任務保存起來,以便以后進行處理。

但是這樣我們只知道了還沒有開始執行的任務,對于那些已經開始執行但是沒有執行完畢卻被取消的任務我們無法獲取。

我們看下如何獲得開始執行但是還沒有執行完畢的任務:

public class TrackingExecutor extends AbstractExecutorService {private final ExecutorService executorService;private final Set<Runnable> taskCancelledAtShutdown= Collections.synchronizedSet(new HashSet<Runnable>());public TrackingExecutor(ExecutorService executorService){this.executorService=executorService;}@Overridepublic void shutdown() {executorService.shutdown();}@Overridepublic List<Runnable> shutdownNow() {return executorService.shutdownNow();}@Overridepublic boolean isShutdown() {return executorService.isShutdown();}@Overridepublic boolean isTerminated() {return executorService.isTerminated();}@Overridepublic boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {return executorService.awaitTermination(timeout,unit);}@Overridepublic void execute(Runnable command) {executorService.execute(() -> {try {command.run();}finally {if(isShutdown() && Thread.currentThread().isInterrupted()){taskCancelledAtShutdown.add(command);}}});}public List<Runnable> getCancelledTask(){if(! executorService.isTerminated()){throw new IllegalStateException("executorService is not terminated");}return new ArrayList<>(taskCancelledAtShutdown);} }

上面的例子中我們構建了一個新的ExecutorService,他傳入一個ExecutorService,并對其進行封裝。

我們重寫了execute方法,在執行完畢判斷該任務是否被中斷,如果被中斷則將其添加到CancelledTask列表中。

并提供一個getCancelledTask方法來返回未執行完畢的任務。

我們看下怎么使用:

public void useShutdownNow() throws InterruptedException {TrackingExecutor trackingExecutor=new TrackingExecutor(Executors.newCachedThreadPool());Runnable runnableTask = () -> {try {TimeUnit.MILLISECONDS.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}};trackingExecutor.submit(runnableTask);List<Runnable> notrunList=trackingExecutor.shutdownNow();if(trackingExecutor.awaitTermination(800, TimeUnit.SECONDS)){List<Runnable> runButCancelledList= trackingExecutor.getCancelledTask();}}

trackingExecutor.shutdownNow()返回的是未執行的任務。而trackingExecutor.getCancelledTask()返回的是被取消的任務。

上面的任務其實還有一個缺點,因為我們在存儲被取消的任務列表的額時候taskCancelledAtShutdown.add(command),因為之前的判斷不是原子操作,則可能會產生誤報。

本文的例子請參考https://github.com/ddean2009/learn-java-concurrency/tree/master/ExecutorServiceShutdown

更多內容請參考http://www.flydean.com/java-shutdown-executorservice/

總結

以上是生活随笔為你收集整理的使用ExecutorService来停止线程服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。