Java使用ExecutorService來(lái)停止線(xiàn)程服務(wù)
使用ExecutorService來(lái)停止線(xiàn)程服務(wù)
之前的文章中我們提到了ExecutorService可以使用shutdown和shutdownNow來(lái)關(guān)閉。
這兩種關(guān)閉的區(qū)別在于各自的安全性和響應(yīng)性。shutdownNow強(qiáng)行關(guān)閉速度更快,但是風(fēng)險(xiǎn)也更大,因?yàn)槿蝿?wù)可能正在執(zhí)行的過(guò)程中被結(jié)束了。而shutdown正常關(guān)閉雖然速度比較慢,但是卻更安全,因?yàn)樗恢钡鹊疥?duì)列中的所有任務(wù)都執(zhí)行完畢之后才關(guān)閉。
使用shutdown
我們先看一個(gè)使用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將會(huì)阻塞直到所有正在執(zhí)行的任務(wù)完成,或者達(dá)到指定的timeout時(shí)間。
使用shutdownNow
當(dāng)通過(guò)shutdownNow來(lái)強(qiáng)行關(guān)閉ExecutorService是, 它會(huì)嘗試取消正在執(zhí)行的任務(wù),并返回所有已經(jīng)提交但是還沒(méi)有開(kāi)始的任務(wù)。從而可以將這些任務(wù)保存起來(lái),以便以后進(jìn)行處理。
但是這樣我們只知道了還沒(méi)有開(kāi)始執(zhí)行的任務(wù),對(duì)于那些已經(jīng)開(kāi)始執(zhí)行但是沒(méi)有執(zhí)行完畢卻被取消的任務(wù)我們無(wú)法獲取。
我們看下如何獲得開(kāi)始執(zhí)行但是還沒(méi)有執(zhí)行完畢的任務(wù):
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)建了一個(gè)新的ExecutorService,他傳入一個(gè)ExecutorService,并對(duì)其進(jìn)行封裝。
我們重寫(xiě)了execute方法,在執(zhí)行完畢判斷該任務(wù)是否被中斷,如果被中斷則將其添加到CancelledTask列表中。
并提供一個(gè)getCancelledTask方法來(lái)返回未執(zhí)行完畢的任務(wù)。
我們看下怎么使用:
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í)行的任務(wù)。而trackingExecutor.getCancelledTask()返回的是被取消的任務(wù)。
上面的任務(wù)其實(shí)還有一個(gè)缺點(diǎn),因?yàn)槲覀冊(cè)诖鎯?chǔ)被取消的任務(wù)列表的額時(shí)候taskCancelledAtShutdown.add(command),因?yàn)橹暗呐袛嗖皇窃硬僮鳎瑒t可能會(huì)產(chǎn)生誤報(bào)。
本文的例子請(qǐng)參考https://github.com/ddean2009/learn-java-concurrency/tree/master/ExecutorServiceShutdown
到此這篇關(guān)于Java使用ExecutorService來(lái)停止線(xiàn)程服務(wù)的文章就介紹到這了,更多相關(guān)Java ExecutorService停止線(xiàn)程內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼2. JS語(yǔ)法也可以有C#的switch表達(dá)式3. JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析4. python 利用toapi庫(kù)自動(dòng)生成api5. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)6. 詳解Struts2中對(duì)未登錄jsp頁(yè)面實(shí)現(xiàn)攔截功能7. 老生常談.NET中的 COM 組件8. CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
