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類圖結(jié)構(gòu)
三、Executors常用的方法
- public static ExecutorService newFixedThreadPool(int nThreads) 一種線程數(shù)量固定的線程池,當(dāng)線程處于空閑狀態(tài)時(shí),他們并不會(huì)被回收,除非線程池被關(guān)閉。當(dāng)所有的線程都處于活動(dòng)狀態(tài)時(shí),新的任務(wù)都會(huì)處于等待狀態(tài),直到有線程空閑出來。
- public static ExecutorService newSingleThreadExecutor() 創(chuàng)建單個(gè)線程。它適用于需要保證順序地執(zhí)行各個(gè)任務(wù);并且在任意時(shí)間點(diǎn),不會(huì)有多個(gè)線程是活動(dòng)的應(yīng)用場(chǎng)景。
- public static ExecutorService newCachedThreadPool() 創(chuàng)建一個(gè)根據(jù)需要?jiǎng)?chuàng)建新線程的線程池,但在可用時(shí)將重新使用以前構(gòu)造的線程, 如果沒有可用的線程,將創(chuàng)建一個(gè)新的線程并將其添加到該池中。 未使用六十秒的線程將被終止并從緩存中刪除。
- public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個(gè)線程池,可以調(diào)度命令在給定的延遲之后運(yùn)行,或定期執(zhí)行, 支持執(zhí)行定時(shí)性或周期性任務(wù)。
- public static ExecutorService newWorkStealingPool(int parallelism) 創(chuàng)建一個(gè)維護(hù)足夠的線程以支持給定的并行級(jí)別的線程池,并且可以使用多個(gè)隊(duì)列來減少爭(zhē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ù)量固定的線程池,線程池?cái)?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(); } } }); } } }
輸出結(jié)果如下圖
結(jié)論:示例中創(chuàng)建了數(shù)量固定為3的線程,由輸出結(jié)果截圖可知,遍歷次數(shù)為5次,當(dāng)執(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)建單個(gè)線程 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(); } } }); } } }
輸出結(jié)果如下圖
結(jié)論:示例中創(chuàng)建了創(chuàng)建單個(gè)線程,每執(zhí)行一次任務(wù)后,睡眠一秒,保證順序地執(zhí)行各個(gè)任務(wù)。
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(); } } }); } } }
輸出結(jié)果如下圖
結(jié)論:示例中根據(jù)需要?jiǎng)?chuàng)建帶有緩存線程的線程池,并在可用時(shí)將重新使用以前構(gòu)造的線程。
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í)行周期性任務(wù)的線程池 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3); /** * schedule(Runnable command,long delay, TimeUnit unit)方法參數(shù)解析 * command 表示執(zhí)行任務(wù)命令 * delay 表示從現(xiàn)在開始延遲執(zhí)行的時(shí)間 * unit 延時(shí)參數(shù)的時(shí)間單位 */ scheduledThreadPool.schedule(new Runnable() { @Override public void run() { System.out.println("scheduledThreadPool:"+LocalDateTime.now()); } },1L, TimeUnit.MINUTES); System.out.println("當(dāng)前時(shí)間:"+LocalDateTime.now()); } }
輸出結(jié)果如下圖
結(jié)論:示例中創(chuàng)建執(zhí)行周期性或定時(shí)性任務(wù)的線程池,由輸出結(jié)果可知,設(shè)置的1分鐘后執(zhí)行任務(wù)已經(jīng)生效。
五、Executors創(chuàng)建線程池原理
1、無論是創(chuàng)建何種類型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會(huì)調(diào)用ThreadPoolExecutor構(gòu)造函數(shù)。
2、 ThreadPoolExecutor構(gòu)造函數(shù)中的參數(shù)解析
- corePoolSize 核心線程最大數(shù)量,通俗點(diǎn)來講就是,線程池中常駐線程的最大數(shù)量
- maximumPoolSize 線程池中運(yùn)行最大線程數(shù)(包括核心線程和非核心線程)
- keepAliveTime 線程池中空閑線程(僅適用于非核心線程)所能存活的最長(zhǎng)時(shí)間
- unit 存活時(shí)間單位,與keepAliveTime搭配使用
- workQueue 存放任務(wù)的阻塞隊(duì)列
- handler 線程池飽和策略
到此這篇關(guān)于Java并發(fā)編程之Executors類詳解的文章就介紹到這了,更多相關(guān)Java Executors類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java利用cors實(shí)現(xiàn)跨域請(qǐng)求實(shí)例
本篇文章主要介紹了Java利用cors實(shí)現(xiàn)跨域請(qǐng)求實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Spring?Boot實(shí)現(xiàn)JWT?token自動(dòng)續(xù)期的實(shí)現(xiàn)
本文主要介紹了Spring?Boot實(shí)現(xiàn)JWT?token自動(dòng)續(xù)期,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Java Speech API實(shí)現(xiàn)語(yǔ)音識(shí)別
Java語(yǔ)音識(shí)別是一項(xiàng)非常有用的功能,它可以將語(yǔ)音轉(zhuǎn)換為文本,從而實(shí)現(xiàn)語(yǔ)音輸入和語(yǔ)音控制功能,在當(dāng)今數(shù)字化時(shí)代,語(yǔ)音識(shí)別技術(shù)逐漸成為人機(jī)交互的重要方式之一,語(yǔ)音識(shí)別技術(shù)可以幫助我們將語(yǔ)音數(shù)據(jù)轉(zhuǎn)化為文字,進(jìn)而進(jìn)行后續(xù)的處理和分析2023-10-10Hibernate三種狀態(tài)和Session常用的方法
本文主要介紹了Hibernate三種狀態(tài)和Session常用的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03教你利用springboot集成swagger并生成接口文檔
有很多小伙伴不會(huì)利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼圖文介紹及代碼示例,對(duì)不會(huì)這個(gè)方法的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Java實(shí)戰(zhàn)之電影在線觀看系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)電影在線觀看系統(tǒng),文中用到的技術(shù)有:JSP、Spring、SpringMVC、MyBatis等,感興趣的可以了解一下2022-04-04SpringBoot+SpringCloud用戶信息微服務(wù)傳遞實(shí)現(xiàn)解析
這篇文章主要介紹了SpringBoot+SpringCloud實(shí)現(xiàn)登錄用戶信息在微服務(wù)之間的傳遞,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11你知道怎么用Spring的三級(jí)緩存解決循環(huán)依賴嗎
這篇文章主要為大家詳細(xì)介紹了Spring的三級(jí)緩存解決循環(huán)依賴,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02