解讀線程池-Executors的newSingleThreadExecutor和newFixedThreadPool(1)區(qū)別
線程池Executors的newSingleThreadExecutor和newFixedThreadPool(1)
與其他等效的newFixedThreadPool(1)不同
- newSingleThreadExecutor返回的執(zhí)行器保證不可重新配置。
與其他等效的newScheduledThreadPool(1)不同
- newSingleThreadScheduledExecutor返回的執(zhí)行器保證不可重新配置以使用其他線程。
newFixedThreadPool(1)的返回結(jié)果我們可以通過(guò)強(qiáng)轉(zhuǎn)變成ThreadPoolExecutor,但是這個(gè)類是可以自行指定線程數(shù)的。
我們可以通過(guò)setCorePoolSize方法來(lái)修改。
這樣也就是說(shuō),這兩個(gè)方法的最大的區(qū)別是第一個(gè)方法可以修改線程的數(shù)量,如果用來(lái)指定線程數(shù)量為1是不安全的。
newSingleThreadExecutor方法則通過(guò)提供了一個(gè)包裝類完全堵住了這個(gè)漏洞。
舉個(gè)例子
拿newSingleThreadExecutor和newFixedThreadPool(1)舉例
import org.junit.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class ThreadPoolDemo { static class MyRunnable implements Runnable { @Override public void run() { System.out.println("開始"); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("結(jié)束" + Thread.currentThread().getName()); } } @Test public void test() throws InterruptedException { //創(chuàng)建Runnable實(shí)例對(duì)象 MyRunnable r = new MyRunnable(); //創(chuàng)建線程池對(duì)象 System.out.println("fixedThreadPool"); ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);//包含1個(gè)線程對(duì)象 for (int i = 0; i < 10; i++) { fixedThreadPool.submit(r); } Thread.sleep(10000); /** * 以上輸出: * fixedThreadPool * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 * 開始 * 結(jié)束pool-1-thread-1 */ System.out.println("singleThreadExecutor"); ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { singleThreadExecutor.submit(r); } Thread.sleep(10000); /** * 以上輸出: * singleThreadExecutor * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 * 開始 * 結(jié)束pool-2-thread-1 */ System.out.println("fixedThreadPool(5)"); ((ThreadPoolExecutor) fixedThreadPool).setCorePoolSize(5); for (int i = 0; i < 10; i++) { fixedThreadPool.submit(r); } Thread.sleep(10000); /** * 以上輸出: * fixedThreadPool(5) * 開始 * 開始 * 開始 * 開始 * 開始 * 結(jié)束pool-1-thread-1 * 結(jié)束pool-1-thread-5 * 結(jié)束pool-1-thread-2 * 結(jié)束pool-1-thread-3 * 結(jié)束pool-1-thread-4 * 開始 * 結(jié)束pool-1-thread-4 * 開始 * 結(jié)束pool-1-thread-4 * 開始 * 結(jié)束pool-1-thread-4 * 開始 * 結(jié)束pool-1-thread-4 * 開始 * 結(jié)束pool-1-thread-4 * singleThreadExecutor(5) */ System.out.println("singleThreadExecutor(5)"); ((ThreadPoolExecutor) singleThreadExecutor).setCorePoolSize(5); for (int i = 0; i < 10; i++) { singleThreadExecutor.submit(r); } /** * 以下輸出: * Exception in thread "main" java.lang.ClassCastException: java.util.concurrent.Executors$FinalizableDelegatedExecutorService cannot be cast to java.util.concurrent.ThreadPoolExecutor * at ThreadPoolDemo.main(ThreadPoolDemo.java:33) */ } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
@Scheduled 如何讀取動(dòng)態(tài)配置文件
這篇文章主要介紹了@Scheduled 如何讀取動(dòng)態(tài)配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring Boot集成Spring Cache過(guò)程詳解
這篇文章主要介紹了Spring Boot集成Spring Cache過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Java動(dòng)態(tài)字節(jié)碼注入技術(shù)的實(shí)現(xiàn)
Java動(dòng)態(tài)字節(jié)碼注入技術(shù)是一種在運(yùn)行時(shí)修改Java字節(jié)碼的技術(shù),本文主要介紹了Java動(dòng)態(tài)字節(jié)碼注入技術(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Java基于中介者模式實(shí)現(xiàn)多人聊天室功能示例
這篇文章主要介紹了Java基于中介者模式實(shí)現(xiàn)多人聊天室功能,詳細(xì)分析了中介者模式的概念、原理以及使用中介模式實(shí)現(xiàn)多人聊天的步驟、操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05idea中斷點(diǎn)類型之All和Thread的區(qū)別介紹
使用all模式對(duì)于程序中含有多個(gè)線程來(lái)說(shuō),會(huì)將多個(gè)線程都阻塞在斷點(diǎn),此時(shí)所有的線程都執(zhí)行到此處,在最后一個(gè)線程執(zhí)行到此處是會(huì)發(fā)生暫停,在這之前的線程會(huì)繼續(xù)執(zhí)行到任意位置,本文給大家詳細(xì)介紹下idea中斷點(diǎn)類型之All和Thread的區(qū)別,感興趣的朋友一起看看吧2022-03-03