Java并發(fā)編程之Executors類詳解
一、Executors的理解
- Executors類屬于java.util.concurrent包;
- 線程池的創(chuàng)建分為兩種方式:ThreadPoolExecutor 和 Executors;
- Executors(靜態(tài)Executor工廠)用于創(chuàng)建線程池;
- 工廠和工具方法Executor , ExecutorService , ScheduledExecutorService , ThreadFactory和Callable在此包中定義的類;
jdk1.8API中的解釋如下:
二、Executors類圖結構
三、Executors常用的方法
- public static ExecutorService newFixedThreadPool(int nThreads) 一種線程數(shù)量固定的線程池,當線程處于空閑狀態(tài)時,他們并不會被回收,除非線程池被關閉。當所有的線程都處于活動狀態(tài)時,新的任務都會處于等待狀態(tài),直到有線程空閑出來。
- public static ExecutorService newSingleThreadExecutor() 創(chuàng)建單個線程。它適用于需要保證順序地執(zhí)行各個任務;并且在任意時間點,不會有多個線程是活動的應用場景。
- public static ExecutorService newCachedThreadPool() 創(chuàng)建一個根據(jù)需要創(chuàng)建新線程的線程池,但在可用時將重新使用以前構造的線程, 如果沒有可用的線程,將創(chuàng)建一個新的線程并將其添加到該池中。 未使用六十秒的線程將被終止并從緩存中刪除。
- public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個線程池,可以調度命令在給定的延遲之后運行,或定期執(zhí)行, 支持執(zhí)行定時性或周期性任務。
- public static ExecutorService newWorkStealingPool(int parallelism) 創(chuàng)建一個維護足夠的線程以支持給定的并行級別的線程池,并且可以使用多個隊列來減少爭用。 ( jdk1.8版本新增的方法 )
四、Executors類中常用方法示例
1、newFixedThreadPool方法示例
代碼
package com.xz.thread.executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @description: * @author: xz * @create: 2021-06-16 21:33 */ public class Demo { public static void main(String[] args) { //創(chuàng)建數(shù)量固定的線程池,線程池數(shù)量為3 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); for(int i=0;i<5;i++){ fixedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); try { Thread.sleep(500); System.out.println("睡眠一秒"); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
輸出結果如下圖
結論:示例中創(chuàng)建了數(shù)量固定為3的線程,由輸出結果截圖可知,遍歷次數(shù)為5次,當執(zhí)行一輪(3次)后,停頓一秒鐘,直到有線程空閑出來,才繼續(xù)第4次執(zhí)行。
2、newSingleThreadExecutor方法示例
代碼
package com.xz.thread.executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @description: * @author: xz * @create: 2021-06-15 22:33 */ public class Demo { public static void main(String[] args) { //創(chuàng)建單個線程 ExecutorService singleThreadPool = Executors.newSingleThreadExecutor(); for(int i=0;i<5;i++){ singleThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); try { Thread.sleep(1000); System.out.println("睡眠一秒"); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
輸出結果如下圖
結論:示例中創(chuàng)建了創(chuàng)建單個線程,每執(zhí)行一次任務后,睡眠一秒,保證順序地執(zhí)行各個任務。
3、newCachedThreadPool方法 代碼
package com.xz.thread.executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @description: * @author: xz * @create: 2021-06-15 22:33 */ public class Demo { public static void main(String[] args) { //創(chuàng)建帶有緩存功能的線程池 ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for(int i=0;i<5;i++){ cachedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); try { Thread.sleep(1000); System.out.println("睡眠一秒"); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
輸出結果如下圖
結論:示例中根據(jù)需要創(chuàng)建帶有緩存線程的線程池,并在可用時將重新使用以前構造的線程。
4、newScheduledThreadPool方法示例
代碼
package com.xz.thread.executor; import java.time.LocalDateTime; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @description: * @author: xz * @create: 2021-06-15 22:33 */ public class Demo { public static void main(String[] args) { //創(chuàng)建執(zhí)行周期性任務的線程池 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3); /** * schedule(Runnable command,long delay, TimeUnit unit)方法參數(shù)解析 * command 表示執(zhí)行任務命令 * delay 表示從現(xiàn)在開始延遲執(zhí)行的時間 * unit 延時參數(shù)的時間單位 */ scheduledThreadPool.schedule(new Runnable() { @Override public void run() { System.out.println("scheduledThreadPool:"+LocalDateTime.now()); } },1L, TimeUnit.MINUTES); System.out.println("當前時間:"+LocalDateTime.now()); } }
輸出結果如下圖
結論:示例中創(chuàng)建執(zhí)行周期性或定時性任務的線程池,由輸出結果可知,設置的1分鐘后執(zhí)行任務已經(jīng)生效。
五、Executors創(chuàng)建線程池原理
1、無論是創(chuàng)建何種類型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會調用ThreadPoolExecutor構造函數(shù)。
2、 ThreadPoolExecutor構造函數(shù)中的參數(shù)解析
- corePoolSize 核心線程最大數(shù)量,通俗點來講就是,線程池中常駐線程的最大數(shù)量
- maximumPoolSize 線程池中運行最大線程數(shù)(包括核心線程和非核心線程)
- keepAliveTime 線程池中空閑線程(僅適用于非核心線程)所能存活的最長時間
- unit 存活時間單位,與keepAliveTime搭配使用
- workQueue 存放任務的阻塞隊列
- handler 線程池飽和策略
到此這篇關于Java并發(fā)編程之Executors類詳解的文章就介紹到這了,更多相關Java Executors類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Spring?Boot實現(xiàn)JWT?token自動續(xù)期的實現(xiàn)
本文主要介紹了Spring?Boot實現(xiàn)JWT?token自動續(xù)期,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Hibernate三種狀態(tài)和Session常用的方法
本文主要介紹了Hibernate三種狀態(tài)和Session常用的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03教你利用springboot集成swagger并生成接口文檔
有很多小伙伴不會利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細的代碼圖文介紹及代碼示例,對不會這個方法的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Java實戰(zhàn)之電影在線觀看系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java實現(xiàn)電影在線觀看系統(tǒng),文中用到的技術有:JSP、Spring、SpringMVC、MyBatis等,感興趣的可以了解一下2022-04-04SpringBoot+SpringCloud用戶信息微服務傳遞實現(xiàn)解析
這篇文章主要介紹了SpringBoot+SpringCloud實現(xiàn)登錄用戶信息在微服務之間的傳遞,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11你知道怎么用Spring的三級緩存解決循環(huán)依賴嗎
這篇文章主要為大家詳細介紹了Spring的三級緩存解決循環(huán)依賴,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02