Java使用ExecutorService來停止線程服務
使用ExecutorService來停止線程服務
之前的文章中我們提到了ExecutorService可以使用shutdown和shutdownNow來關閉。
這兩種關閉的區(qū)別在于各自的安全性和響應性。shutdownNow強行關閉速度更快,但是風險也更大,因為任務可能正在執(zhí)行的過程中被結(jié)束了。而shutdown正常關閉雖然速度比較慢,但是卻更安全,因為它一直等到隊列中的所有任務都執(zhí)行完畢之后才關閉。
使用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將會阻塞直到所有正在執(zhí)行的任務完成,或者達到指定的timeout時間。
使用shutdownNow
當通過shutdownNow來強行關閉ExecutorService是, 它會嘗試取消正在執(zhí)行的任務,并返回所有已經(jīng)提交但是還沒有開始的任務。從而可以將這些任務保存起來,以便以后進行處理。
但是這樣我們只知道了還沒有開始執(zhí)行的任務,對于那些已經(jīng)開始執(zhí)行但是沒有執(zhí)行完畢卻被取消的任務我們無法獲取。
我們看下如何獲得開始執(zhí)行但是還沒有執(zhí)行完畢的任務:
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; } @Override public void shutdown() { executorService.shutdown(); } @Override public List<Runnable> shutdownNow() { return executorService.shutdownNow(); } @Override public boolean isShutdown() { return executorService.isShutdown(); } @Override public boolean isTerminated() { return executorService.isTerminated(); } @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return executorService.awaitTermination(timeout,unit); } @Override public 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); }}
上面的例子中我們構(gòu)建了一個新的ExecutorService,他傳入一個ExecutorService,并對其進行封裝。
我們重寫了execute方法,在執(zhí)行完畢判斷該任務是否被中斷,如果被中斷則將其添加到CancelledTask列表中。
并提供一個getCancelledTask方法來返回未執(zhí)行完畢的任務。
我們看下怎么使用:
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()返回的是未執(zhí)行的任務。而trackingExecutor.getCancelledTask()返回的是被取消的任務。
上面的任務其實還有一個缺點,因為我們在存儲被取消的任務列表的額時候taskCancelledAtShutdown.add(command),因為之前的判斷不是原子操作,則可能會產(chǎn)生誤報。
本文的例子請參考https://github.com/ddean2009/learn-java-concurrency/tree/master/ExecutorServiceShutdown
到此這篇關于Java使用ExecutorService來停止線程服務的文章就介紹到這了,更多相關Java ExecutorService停止線程內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. 資深程序員:給Python軟件開發(fā)測試的25個忠告!2. JavaScript實現(xiàn)留言板實戰(zhàn)案例3. 使用Blazor框架實現(xiàn)在前端瀏覽器中導入和導出Excel4. Python-openpyxl表格讀取寫入的案例詳解5. 如何在python中執(zhí)行另一個py文件6. 實現(xiàn)java簡單的線程池7. Python基于requests庫爬取網(wǎng)站信息8. 如何從Python的cmd中獲得.py文件參數(shù)9. ASP基礎知識Command對象講解10. vscode運行php報錯php?not?found解決辦法

網(wǎng)公網(wǎng)安備