Java并發(fā)編程之Executors類(lèi)詳解
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ù)量固定的線程池,線程池?cái)?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次)后,停頓一秒鐘,直到有線程空閑出來(lái),才繼續(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() {@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)建單個(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() {@Overridepublic 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)在開(kāi)始延遲執(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、無(wú)論是創(chuàng)建何種類(lèi)型線程池(newFixedThreadPool、newSingleThreadExecutor、newCachedThreadPool等等),均會(huì)調(diào)用ThreadPoolExecutor構(gòu)造函數(shù)。
2、 ThreadPoolExecutor構(gòu)造函數(shù)中的參數(shù)解析
corePoolSize 核心線程最大數(shù)量,通俗點(diǎn)來(lái)講就是,線程池中常駐線程的最大數(shù)量 maximumPoolSize 線程池中運(yùn)行最大線程數(shù)(包括核心線程和非核心線程) keepAliveTime 線程池中空閑線程(僅適用于非核心線程)所能存活的最長(zhǎng)時(shí)間 unit 存活時(shí)間單位,與keepAliveTime搭配使用 workQueue 存放任務(wù)的阻塞隊(duì)列 handler 線程池飽和策略到此這篇關(guān)于Java并發(fā)編程之Executors類(lèi)詳解的文章就介紹到這了,更多相關(guān)Java Executors類(lèi)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明3. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向4. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法5. PHP設(shè)計(jì)模式中工廠模式深入詳解6. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析7. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. 如何基于Python Matplotlib實(shí)現(xiàn)網(wǎng)格動(dòng)畫(huà)10. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)
