這篇文章將為大家詳細(xì)講解有關(guān)Java中ThreadPoolExecutor線程池的使用方法,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元弋陽做網(wǎng)站,已為上家服務(wù),為弋陽各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
Executors
Executors 是一個Java中的工具類. 提供工廠方法來創(chuàng)建不同類型的線程池.
從上圖中也可以看出, Executors的創(chuàng)建線程池的方法, 創(chuàng)建出來的線程池都實(shí)現(xiàn)了 ExecutorService接口. 常用方法有以下幾個:
newFixedThreadPool(int Threads): 創(chuàng)建固定數(shù)目線程的線程池, 超出的線程會在隊(duì)列中等待.
newCachedThreadPool(): 創(chuàng)建一個可緩存線程池, 如果線程池長度超過處理需要, 可靈活回收空閑線程(60秒), 若無可回收,則新建線程.
newSingleThreadExecutor(): 創(chuàng)建一個單線程化的線程池, 它只會用唯一的工作線程來執(zhí)行任務(wù), 保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行. 如果某一個任務(wù)執(zhí)行出錯, 將有另一個線程來繼續(xù)執(zhí)行.
newScheduledThreadPool(int corePoolSize): 創(chuàng)建一個支持定時(shí)及周期性的任務(wù)執(zhí)行的線程池, 多數(shù)情況下可用來替代Timer類.
Executors 例子
newCachedThreadPool
線程最大數(shù)為 Integer.MAX_VALUE, 當(dāng)我們往線程池添加了 n 個任務(wù), 這 n 個任務(wù)都是一起執(zhí)行的.
ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); cachedThreadPool.execute(new Runnable() { @Override public void run() { for (;;) { try { Thread.currentThread().sleep(1000); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }); cachedThreadPool.execute(new Runnable() { @Override public void run() { for (;;) { try { Thread.currentThread().sleep(1000); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } });
ExecutorService cachedThreadPool = Executors.newFixedThreadPool(1); cachedThreadPool.execute(new Runnable() { @Override public void run() { for (;;) { try { Thread.currentThread().sleep(1000); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }); cachedThreadPool.execute(new Runnable() { @Override public void run() { for (;;) { try { Thread.currentThread().sleep(1000); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } });
三秒執(zhí)行一次, 只有執(zhí)行完這一次后, 才會執(zhí)行.
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5); scheduledExecutorService.schedule(new Runnable() { @Override public void run() { for (;;) { try { Thread.currentThread().sleep(2000); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }, 3, TimeUnit.SECONDS);
順序執(zhí)行各個任務(wù), 第一個任務(wù)執(zhí)行完, 才會執(zhí)行下一個.
ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new Runnable() { @Override public void run() { for (;;) { try { System.out.println(Thread.currentThread().getName()); Thread.currentThread().sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); executorService.execute(new Runnable() { @Override public void run() { for (;;) { try { System.out.println(Thread.currentThread().getName()); Thread.currentThread().sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } });
Executors存在什么問題
在阿里巴巴Java開發(fā)手冊中提到,使用Executors創(chuàng)建線程池可能會導(dǎo)致OOM(OutOfMemory ,內(nèi)存溢出),但是并沒有說明為什么,那么接下來我們就來看一下到底為什么不允許使用Executors?
我們先來一個簡單的例子,模擬一下使用Executors導(dǎo)致OOM的情況.
/** * @author Hollis */ public class ExecutorsDemo { private static ExecutorService executor = Executors.newFixedThreadPool(15); public static void main(String[] args) { for (int i = 0; i < Integer.MAX_VALUE; i++) { executor.execute(new SubThread()); } } } class SubThread implements Runnable { @Override public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) { //do nothing } } }
通過指定JVM參數(shù):-Xmx8m -Xms8m 運(yùn)行以上代碼,會拋出OOM:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded at java.util.concurrent.LinkedBlockingQueue.offer(LinkedBlockingQueue.java:416) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1371) at com.hollis.ExecutorsDemo.main(ExecutorsDemo.java:16)
以上代碼指出,ExecutorsDemo.java 的第16行,就是代碼中的 executor.execute(new SubThread());
Java中的 BlockingQueue 主要有兩種實(shí)現(xiàn), 分別是 ArrayBlockingQueue 和 LinkedBlockingQueue.
ArrayBlockingQueue 是一個用數(shù)組實(shí)現(xiàn)的有界阻塞隊(duì)列, 必須設(shè)置容量.
public ArrayBlockingQueue(int capacity, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); this.items = new Object[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); }
LinkedBlockingQueue 是一個用鏈表實(shí)現(xiàn)的有界阻塞隊(duì)列, 容量可以選擇進(jìn)行設(shè)置, 不設(shè)置的話, 將是一個無邊界的阻塞隊(duì)列, 最大長度為 Integer.MAX_VALUE.
public LinkedBlockingQueue() { this(Integer.MAX_VALUE); }
這里的問題就出在如果我們不設(shè)置 LinkedBlockingQueue 的容量的話, 其默認(rèn)容量將會是 Integer.MAX_VALUE.
而 newFixedThreadPool 中創(chuàng)建 LinkedBlockingQueue 時(shí), 并未指定容量. 此時(shí), LinkedBlockingQueue 就是一個無邊界隊(duì)列, 對于一個無邊界隊(duì)列來說, 是可以不斷的向隊(duì)列中加入任務(wù)的, 這種情況下就有可能因?yàn)槿蝿?wù)過多而導(dǎo)致內(nèi)存溢出問題.
newCachedThreadPool 和 newScheduledThreadPool 這兩種方式創(chuàng)建的最大線程數(shù)可能是Integer.MAX_VALUE, 而創(chuàng)建這么多線程, 必然就有可能導(dǎo)致OOM.
ThreadPoolExecutor 創(chuàng)建線程池
避免使用 Executors 創(chuàng)建線程池, 主要是避免使用其中的默認(rèn)實(shí)現(xiàn), 那么我們可以自己直接調(diào)用 ThreadPoolExecutor 的構(gòu)造函數(shù)來自己創(chuàng)建線程池. 在創(chuàng)建的同時(shí), 給 BlockQueue 指定容量就可以了.
ExecutorService executor = new ThreadPoolExecutor(10, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(10));
這種情況下, 一旦提交的線程數(shù)超過當(dāng)前可用線程數(shù)時(shí), 就會拋出 java.util.concurrent.RejectedExecutionException, 這是因?yàn)楫?dāng)前線程池使用的隊(duì)列是有邊界隊(duì)列, 隊(duì)列已經(jīng)滿了便無法繼續(xù)處理新的請求.
除了自己定義 ThreadPoolExecutor 外. 還有其他方法. 如apache和guava等.
四個構(gòu)造函數(shù)
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
int corePoolSize => 該線程池中核心線程數(shù)最大值
線程池新建線程的時(shí)候,如果當(dāng)前線程總數(shù)小于corePoolSize, 則新建的是核心線程, 如果超過corePoolSize, 則新建的是非核心線程
核心線程默認(rèn)情況下會一直存活在線程池中, 即使這個核心線程啥也不干(閑置狀態(tài)).
如果指定 ThreadPoolExecutor 的 allowCoreThreadTimeOut 這個屬性為 true, 那么核心線程如果不干活(閑置狀態(tài))的話, 超過一定時(shí)間(時(shí)長下面參數(shù)決定), 就會被銷毀掉
很好理解吧, 正常情況下你不干活我也養(yǎng)你, 因?yàn)槲铱傆杏玫侥愕臅r(shí)候, 但有時(shí)候特殊情況(比如我自己都養(yǎng)不起了), 那你不干活我就要把你干掉了
int maximumPoolSize
該線程池中線程總數(shù)最大值
線程總數(shù) = 核心線程數(shù) + 非核心線程數(shù).
long keepAliveTime
該線程池中非核心線程閑置超時(shí)時(shí)長
一個非核心線程, 如果不干活(閑置狀態(tài))的時(shí)長超過這個參數(shù)所設(shè)定的時(shí)長, 就會被銷毀掉
如果設(shè)置 allowCoreThreadTimeOut = true, 則會作用于核心線程
TimeUnit unit
keepAliveTime的單位, TimeUnit是一個枚舉類型, 其包括:
TimeUnit.DAYS; //天 TimeUnit.HOURS; //小時(shí) TimeUnit.MINUTES; //分鐘 TimeUnit.SECONDS; //秒 TimeUnit.MILLISECONDS; //毫秒 TimeUnit.MICROSECONDS; //微妙 TimeUnit.NANOSECONDS; //納秒
BlockingQueue workQueue
一個阻塞隊(duì)列, 用來存儲等待執(zhí)行的任務(wù). 也就是說現(xiàn)在有10個任務(wù), 核心線程 有四個, 非核心線程有六個, 那么這六個線程會被添加到 workQueue 中, 等待執(zhí)行.
這個參數(shù)的選擇也很重要, 會對線程池的運(yùn)行過程產(chǎn)生重大影響, 一般來說, 這里的阻塞隊(duì)列有以下幾種選擇:
SynchronousQueue: 這個隊(duì)列接收到任務(wù)的時(shí)候, 會直接提交給線程處理, 而不保留它, 如果所有線程都在工作怎么辦? 那就*新建一個線程來處理這個任務(wù)!所以為了保證不出現(xiàn)<線程數(shù)達(dá)到了maximumPoolSize而不能新建線程>的錯誤, 使用這個類型隊(duì)列的時(shí)候, maximumPoolSize 一般指定成 Integer.MAX_VALUE, 即無限大.
LinkedBlockingQueue: 這個隊(duì)列接收到任務(wù)的時(shí)候, 如果當(dāng)前線程數(shù)小于核心線程數(shù), 則核心線程處理任務(wù); 如果當(dāng)前線程數(shù)等于核心線程數(shù), 則進(jìn)入隊(duì)列等待. 由于這個隊(duì)列最大值為 Integer.MAX_VALUE , 即所有超過核心線程數(shù)的任務(wù)都將被添加到隊(duì)列中,這也就導(dǎo)致了 maximumPoolSize 的設(shè)定失效, 因?yàn)榭偩€程數(shù)永遠(yuǎn)不會超過 corePoolSize.
ArrayBlockingQueue: 可以限定隊(duì)列的長度, 接收到任務(wù)的時(shí)候, 如果沒有達(dá)到 corePoolSize 的值, 則核心線程執(zhí)行任務(wù), 如果達(dá)到了, 則入隊(duì)等候, 如果隊(duì)列已滿, 則新建線程(非核心線程)執(zhí)行任務(wù), 又如果總線程數(shù)到了maximumPoolSize, 并且隊(duì)列也滿了, 則發(fā)生錯誤.
DelayQueue: 隊(duì)列內(nèi)元素必須實(shí)現(xiàn) Delayed 接口, 這就意味著你傳進(jìn)去的任務(wù)必須先實(shí)現(xiàn)Delayed接口. 這個隊(duì)列接收到任務(wù)時(shí), 首先先入隊(duì), 只有達(dá)到了指定的延時(shí)時(shí)間, 才會執(zhí)行任務(wù).
ThreadFactory threadFactory
它是ThreadFactory類型的變量, 用來創(chuàng)建新線程.
默認(rèn)使用 Executors.defaultThreadFactory()
來創(chuàng)建線程. 使用默認(rèn)的 ThreadFactory
來創(chuàng)建線程時(shí), 會使新創(chuàng)建的線程具有相同的 NORM_PRIORITY
優(yōu)先級并且是非守護(hù)線程, 同時(shí)也設(shè)置了線程的名稱.
RejectedExecutionHandler handler
表示當(dāng)拒絕處理任務(wù)時(shí)的策略, 有以下四種取值:
ThreadPoolExecutor.AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常(默認(rèn)). ThreadPoolExecutor.DiscardPolicy:直接丟棄任務(wù), 但是不拋出異常. ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù), 然后重新嘗試執(zhí)行任務(wù)(重復(fù)此過程) ThreadPoolExecutor.CallerRunsPolicy:用調(diào)用者所在的線程來執(zhí)行任務(wù).
關(guān)于Java中ThreadPoolExecutor線程池的使用方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
標(biāo)題名稱:Java中ThreadPoolExecutor線程池的使用方法
網(wǎng)頁URL:http://muchs.cn/article30/jcphpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、做網(wǎng)站、網(wǎng)站排名、外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)