Java并發(fā)編程之Executors類詳解
jdk1.8API中的解釋如下:
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ù)量為3ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);for(int i=0;i<5;i++){ fixedThreadPool.execute(new Runnable() {@Overridepublic 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)建單個線程ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();for(int i=0;i<5;i++){ singleThreadPool.execute(new Runnable() {@Overridepublic 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)建單個線程,每執(zhí)行一次任務(wù)后,睡眠一秒,保證順序地執(zhí)行各個任務(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() {@Overridepublic void run() { System.out.println(Thread.currentThread().getName()); try {Thread.sleep(1000);System.out.println('睡眠一秒'); } catch (InterruptedException e) {e.printStackTrace(); }} });} }}
輸出結(jié)果如下圖
結(jié)論:示例中根據(jù)需要創(chuàng)建帶有緩存線程的線程池,并在可用時將重新使用以前構(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í)行的時間 * unit 延時參數(shù)的時間單位 */scheduledThreadPool.schedule(new Runnable() { @Override public void run() {System.out.println('scheduledThreadPool:'+LocalDateTime.now()); }},1L, TimeUnit.MINUTES);System.out.println('當(dāng)前時間:'+LocalDateTime.now()); }}
輸出結(jié)果如下圖
結(jié)論:示例中創(chuàng)建執(zhí)行周期性或定時性任務(wù)的線程池,由輸出結(jié)果可知,設(shè)置的1分鐘后執(zhí)行任務(wù)已經(jīng)生效。
五、Executors創(chuàng)建線程池原理1、無論是創(chuàng)建何種類型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會調(diào)用ThreadPoolExecutor構(gòu)造函數(shù)。
2、 ThreadPoolExecutor構(gòu)造函數(shù)中的參數(shù)解析
corePoolSize 核心線程最大數(shù)量,通俗點(diǎn)來講就是,線程池中常駐線程的最大數(shù)量 maximumPoolSize 線程池中運(yùn)行最大線程數(shù)(包括核心線程和非核心線程) keepAliveTime 線程池中空閑線程(僅適用于非核心線程)所能存活的最長時間 unit 存活時間單位,與keepAliveTime搭配使用 workQueue 存放任務(wù)的阻塞隊列 handler 線程池飽和策略到此這篇關(guān)于Java并發(fā)編程之Executors類詳解的文章就介紹到這了,更多相關(guān)Java Executors類內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
