線程池之exectue與submit的區(qū)別及說明
1、參數(shù)及返回值不同
- excute只能提交Runnable,無返回值
- submit既可以提交Runnable,返回值為null,也可以提交Callable,返回值Future
excute:
submit:
2、異常拋出不同
- execute執(zhí)行任務(wù)時(shí)遇到異常會(huì)直接拋出
- submit執(zhí)行任務(wù)是遇到異常不會(huì)直接拋出,只有在使用Future的get方法獲取返回值時(shí)才會(huì)拋出異常
execute:
import org.junit.Test; import java.util.concurrent.*; public class ThreadPoolDemo { @Test public void test() throws InterruptedException, ExecutionException { //創(chuàng)建線程池對(duì)象 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(() -> { System.out.println("開始"); int i = 10 / 0; System.out.println("結(jié)束"); }); } }
輸出:
開始
Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
at ThreadPoolDemo.lambda$test$0(ThreadPoolDemo.java:13)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
submit:
import org.junit.Test; import java.util.concurrent.*; public class ThreadPoolDemo { @Test public void test() throws InterruptedException, ExecutionException { //創(chuàng)建線程池對(duì)象 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.submit(() -> { System.out.println("開始"); int i = 10 / 0; System.out.println("結(jié)束"); }); } }
輸出:
開始
submit 增加Future的get方法
import org.junit.Test; import java.util.concurrent.*; public class ThreadPoolDemo { @Test public void test() throws InterruptedException, ExecutionException { //創(chuàng)建線程池對(duì)象 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); Future future=singleThreadExecutor.submit(() -> { System.out.println("開始"); int i = 10 / 0; System.out.println("結(jié)束"); }); Object o=future.get(); singleThreadExecutor.shutdown(); } }
輸出:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼,通過文字說明結(jié)合實(shí)例的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-05-05Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例
本篇文章主要介紹了Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03MyBatis-Plus自定義通用的方法實(shí)現(xiàn)
MP自帶的條件構(gòu)造器雖然很強(qiáng)大,有時(shí)候也避免不了寫稍微復(fù)雜一點(diǎn)業(yè)務(wù)的sql,本文主要介紹了MyBatis-Plus自定義通用的方法實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05servlet之session工作原理簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了servlet之session工作原理簡介,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07SpringBoot解析JSON數(shù)據(jù)的三種方案
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式,易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,本文給大家介紹了SpringBoot解析JSON數(shù)據(jù)的三種方案,需要的朋友可以參考下2024-03-03SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改
本文主要介紹了SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
這篇文章主要介紹了Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04