Java:線程池(ExecutorService)使用源碼說明

轉自:https://blog.csdn.net/wanghao_0206/article/details/76460877

一恬吕、前提

/**
* 線程運行demo,運行時打出線程id以及傳入線程中參數(shù)
*/
public classThreadRunnerimplementsRunnable{
? ? private final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");

? ? /**
? ? ?* 線程私有屬性,創(chuàng)建線程時創(chuàng)建
? ? ?*/
? ? private Integer num;
? ? public ThreadRunner(Integer num) {
? ? ? ? this.num = num;
? ? }

? ? @Override
? ? public void run() {
? ? ? ? System.out.println("thread:" + Thread.currentThread().getName() + ",time:" + format.format(new Date()) + ",num:" + num);

? ? ? ? try {//使線程睡眠而柑,模擬線程阻塞情況
? ? ? ? ? ? TimeUnit.SECONDS.sleep(1);
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

二寻馏、分類

1、FixedThreadPool-有一個固定大小的線程池

public class FixedThreadPoolDemo {
? ? public static void main(String[] args) {
? ? ? ? ExecutorService pool = Executors.newFixedThreadPool(4);
? ? ? ? for(int i = 0 ; i < 50 ; i++){
? ? ? ? ? ? pool.submit(new ThreadRunner((i + 1)));
? ? ? ? }
? ? ? ? pool.shutdown();
? ? }
}

輸出:

thread:pool-1-thread-2,time:16:14:45.677,num:2
thread:pool-1-thread-4,time:16:14:45.678,num:4
thread:pool-1-thread-3,time:16:14:45.680,num:3
thread:pool-1-thread-1,time:16:14:45.684,num:1
thread:pool-1-thread-4,time:16:14:46.680,num:5
thread:pool-1-thread-2,time:16:14:46.680,num:6
thread:pool-1-thread-3,time:16:14:46.680,num:7
thread:pool-1-thread-1,time:16:14:46.684,num:8
thread:pool-1-thread-4,time:16:14:47.680,num:9
thread:pool-1-thread-2,time:16:14:47.680,num:10
thread:pool-1-thread-3,time:16:14:47.681,num:11
thread:pool-1-thread-1,time:16:14:47.684,num:12
thread:pool-1-thread-4,time:16:14:48.681,num:13
thread:pool-1-thread-2,time:16:14:48.681,num:14
thread:pool-1-thread-3,time:16:14:48.681,num:15
thread:pool-1-thread-1,time:16:14:48.684,num:16
thread:pool-1-thread-4,time:16:14:49.681,num:17
thread:pool-1-thread-2,time:16:14:49.682,num:18
thread:pool-1-thread-3,time:16:14:49.682,num:19
thread:pool-1-thread-1,time:16:14:49.684,num:20
thread:pool-1-thread-4,time:16:14:50.681,num:21
thread:pool-1-thread-2,time:16:14:50.682,num:22
thread:pool-1-thread-3,time:16:14:50.682,num:23
thread:pool-1-thread-1,time:16:14:50.684,num:24
thread:pool-1-thread-4,time:16:14:51.681,num:25
thread:pool-1-thread-2,time:16:14:51.682,num:26
thread:pool-1-thread-3,time:16:14:51.682,num:27
thread:pool-1-thread-1,time:16:14:51.684,num:28
thread:pool-1-thread-4,time:16:14:52.681,num:29
thread:pool-1-thread-2,time:16:14:52.682,num:30
thread:pool-1-thread-3,time:16:14:52.682,num:31
thread:pool-1-thread-1,time:16:14:52.684,num:32
thread:pool-1-thread-4,time:16:14:53.681,num:33
thread:pool-1-thread-2,time:16:14:53.682,num:34
thread:pool-1-thread-3,time:16:14:53.683,num:35
thread:pool-1-thread-1,time:16:14:53.685,num:36
thread:pool-1-thread-2,time:16:14:54.682,num:38
thread:pool-1-thread-4,time:16:14:54.682,num:37
thread:pool-1-thread-3,time:16:14:54.683,num:39
thread:pool-1-thread-1,time:16:14:54.686,num:40
thread:pool-1-thread-2,time:16:14:55.682,num:41
thread:pool-1-thread-4,time:16:14:55.682,num:42
thread:pool-1-thread-3,time:16:14:55.683,num:43
thread:pool-1-thread-1,time:16:14:55.686,num:44
thread:pool-1-thread-2,time:16:14:56.682,num:45
thread:pool-1-thread-4,time:16:14:56.683,num:46
thread:pool-1-thread-3,time:16:14:56.684,num:47
thread:pool-1-thread-1,time:16:14:56.686,num:48
thread:pool-1-thread-2,time:16:14:57.683,num:49
thread:pool-1-thread-4,time:16:14:57.683,num:50

總結:?

- 池中線程數(shù)量固定杖爽,不會發(fā)生變化?

- 使用無界的LinkedBlockingQueue,要綜合考慮生成與消費能力紫皇,生成過剩慰安,可能導致堆內存溢出。?

- 適用一些很穩(wěn)定很固定的正規(guī)并發(fā)線程聪铺,多用于服務器

2化焕、CachedThreadPool

public class CachedThreadPoolDemo {
? ? public static void main(String[] args) {
? ? ? ? ExecutorService pool = Executors.newCachedThreadPool();
? ? ? ? for(int i = 0 ; i < 50 ; i++){
? ? ? ? ? ? pool.submit(new ThreadRunner((i + 1)));
? ? ? ? }
? ? ? ? pool.shutdown();
? ? }
}

輸出:

thread:pool-1-thread-2,time:16:17:21.289,num:2
thread:pool-1-thread-3,time:16:17:21.290,num:3
thread:pool-1-thread-4,time:16:17:21.290,num:4
thread:pool-1-thread-6,time:16:17:21.291,num:6
thread:pool-1-thread-7,time:16:17:21.291,num:7
thread:pool-1-thread-8,time:16:17:21.291,num:8
thread:pool-1-thread-1,time:16:17:21.292,num:1
thread:pool-1-thread-10,time:16:17:21.293,num:10
thread:pool-1-thread-5,time:16:17:21.294,num:5
thread:pool-1-thread-11,time:16:17:21.294,num:11
thread:pool-1-thread-15,time:16:17:21.294,num:15
thread:pool-1-thread-9,time:16:17:21.294,num:9
thread:pool-1-thread-16,time:16:17:21.295,num:16
thread:pool-1-thread-20,time:16:17:21.295,num:20
thread:pool-1-thread-14,time:16:17:21.296,num:14
thread:pool-1-thread-12,time:16:17:21.296,num:12
thread:pool-1-thread-19,time:16:17:21.297,num:19
thread:pool-1-thread-13,time:16:17:21.299,num:13
thread:pool-1-thread-17,time:16:17:21.300,num:17
thread:pool-1-thread-18,time:16:17:21.302,num:18
thread:pool-1-thread-22,time:16:17:21.304,num:22
thread:pool-1-thread-23,time:16:17:21.304,num:23
thread:pool-1-thread-24,time:16:17:21.305,num:24
thread:pool-1-thread-21,time:16:17:21.305,num:21
thread:pool-1-thread-26,time:16:17:21.305,num:26
thread:pool-1-thread-25,time:16:17:21.306,num:25
thread:pool-1-thread-29,time:16:17:21.307,num:29
thread:pool-1-thread-28,time:16:17:21.308,num:28
thread:pool-1-thread-30,time:16:17:21.308,num:30
thread:pool-1-thread-34,time:16:17:21.308,num:34
thread:pool-1-thread-35,time:16:17:21.308,num:35
thread:pool-1-thread-33,time:16:17:21.308,num:33
thread:pool-1-thread-27,time:16:17:21.309,num:27
thread:pool-1-thread-32,time:16:17:21.308,num:32
thread:pool-1-thread-31,time:16:17:21.309,num:31
thread:pool-1-thread-36,time:16:17:21.310,num:36
thread:pool-1-thread-37,time:16:17:21.310,num:37
thread:pool-1-thread-38,time:16:17:21.310,num:38
thread:pool-1-thread-42,time:16:17:21.310,num:42
thread:pool-1-thread-40,time:16:17:21.310,num:40
thread:pool-1-thread-41,time:16:17:21.311,num:41
thread:pool-1-thread-47,time:16:17:21.762,num:47
thread:pool-1-thread-43,time:16:17:21.762,num:43
thread:pool-1-thread-39,time:16:17:21.762,num:39
thread:pool-1-thread-45,time:16:17:21.762,num:45
thread:pool-1-thread-44,time:16:17:21.763,num:44
thread:pool-1-thread-46,time:16:17:21.761,num:46
thread:pool-1-thread-48,time:16:17:21.761,num:48
thread:pool-1-thread-49,time:16:17:21.765,num:49
thread:pool-1-thread-50,time:16:17:21.765,num:50

總結?

- 池中線程時隨著處理數(shù)據(jù)增加而增加?

- 線程數(shù)并不是一直增加,如果有新任務需要執(zhí)行時铃剔,首先查詢池中是否有空閑線程并且還為到空閑截止時間撒桨,如果有,則使用空閑線程键兜,如果沒有凤类,則創(chuàng)建新線程并放入池中。?

- 用于執(zhí)行一些生存期很短的異步型任務普气。不適用于IO等長延時操作谜疤,因為這可能會創(chuàng)建大量線程,導致系統(tǒng)崩潰。?

- 使用SynchronousQueue作為阻塞隊列夷磕,如果有新任務進入隊列履肃,必須隊列中數(shù)據(jù)被其他線程處理,否則會等待坐桩。

3尺棋、SingleThreadExecutor

public class SingleThreadPoolDemo {
? ? public static void main(String[] args) {
? ? ? ? ExecutorService pool = Executors.newSingleThreadExecutor();
? ? ? ? for(int i = 0 ; i < 50 ; i++){
? ? ? ? ? ? pool.submit(new ThreadRunner((i + 1)));
? ? ? ? }
? ? ? ? pool.shutdown();
? ? }
}

輸出:

thread:pool-1-thread-1,time:16:20:10.194,num:1
thread:pool-1-thread-1,time:16:20:11.197,num:2
thread:pool-1-thread-1,time:16:20:12.197,num:3
thread:pool-1-thread-1,time:16:20:13.197,num:4
thread:pool-1-thread-1,time:16:20:14.197,num:5
thread:pool-1-thread-1,time:16:20:15.198,num:6
thread:pool-1-thread-1,time:16:20:16.198,num:7
thread:pool-1-thread-1,time:16:20:17.198,num:8
thread:pool-1-thread-1,time:16:20:18.198,num:9
thread:pool-1-thread-1,time:16:20:19.198,num:10
thread:pool-1-thread-1,time:16:20:20.198,num:11
thread:pool-1-thread-1,time:16:20:21.199,num:12
thread:pool-1-thread-1,time:16:20:22.200,num:13
thread:pool-1-thread-1,time:16:20:23.200,num:14
thread:pool-1-thread-1,time:16:20:24.200,num:15
thread:pool-1-thread-1,time:16:20:25.200,num:16
thread:pool-1-thread-1,time:16:20:26.201,num:17
thread:pool-1-thread-1,time:16:20:27.201,num:18
thread:pool-1-thread-1,time:16:20:28.201,num:19
thread:pool-1-thread-1,time:16:20:29.201,num:20
thread:pool-1-thread-1,time:16:20:30.202,num:21
thread:pool-1-thread-1,time:16:20:31.202,num:22
thread:pool-1-thread-1,time:16:20:32.203,num:23
thread:pool-1-thread-1,time:16:20:33.203,num:24
thread:pool-1-thread-1,time:16:20:34.203,num:25
thread:pool-1-thread-1,time:16:20:35.203,num:26
thread:pool-1-thread-1,time:16:20:36.203,num:27
thread:pool-1-thread-1,time:16:20:37.203,num:28
thread:pool-1-thread-1,time:16:20:38.203,num:29
thread:pool-1-thread-1,time:16:20:39.203,num:30
thread:pool-1-thread-1,time:16:20:40.203,num:31
thread:pool-1-thread-1,time:16:20:41.203,num:32
thread:pool-1-thread-1,time:16:20:42.203,num:33
thread:pool-1-thread-1,time:16:20:43.204,num:34
thread:pool-1-thread-1,time:16:20:44.204,num:35
thread:pool-1-thread-1,time:16:20:45.204,num:36
thread:pool-1-thread-1,time:16:20:46.204,num:37
thread:pool-1-thread-1,time:16:20:47.205,num:38
thread:pool-1-thread-1,time:16:20:48.205,num:39
thread:pool-1-thread-1,time:16:20:49.205,num:40
thread:pool-1-thread-1,time:16:20:50.206,num:41
thread:pool-1-thread-1,time:16:20:51.206,num:42
thread:pool-1-thread-1,time:16:20:52.207,num:43
thread:pool-1-thread-1,time:16:20:53.207,num:44
thread:pool-1-thread-1,time:16:20:54.207,num:45
thread:pool-1-thread-1,time:16:20:55.207,num:46
thread:pool-1-thread-1,time:16:20:56.207,num:47
thread:pool-1-thread-1,time:16:20:57.208,num:48
thread:pool-1-thread-1,time:16:20:58.208,num:49
thread:pool-1-thread-1,time:16:20:59.209,num:50

總結:?

- 線程中只有一個線程在執(zhí)行?

- 適用于有明確執(zhí)行順序但是不影響主線程的任務,壓入池中的任務會按照隊列順序執(zhí)行撕攒。?

- 使用無界的LinkedBlockingQueue陡鹃,要綜合考慮生成與消費能力,生成過剩抖坪,可能導致堆內存溢出萍鲸。

三、源碼

public static ExecutorService newFixedThreadPool(int nThreads) {
? ? ? ? return new ThreadPoolExecutor(nThreads, nThreads,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0L, TimeUnit.MILLISECONDS,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new LinkedBlockingQueue());

public static ExecutorService newCachedThreadPool() {
? ? ? ? return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 60L, TimeUnit.SECONDS,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new SynchronousQueue());

public static ExecutorService newSingleThreadExecutor() {
? ? ? ? return new FinalizableDelegatedExecutorService
? ? ? ? ? ? (new ThreadPoolExecutor(1, 1,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0L, TimeUnit.MILLISECONDS,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new LinkedBlockingQueue()));
? ? }

* ThreadPoolExecutor 構造方法*

public ThreadPoolExecutor(int corePoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? int maximumPoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? long keepAliveTime,
? ? ? ? ? ? ? ? ? ? ? ? ? TimeUnit unit,
? ? ? ? ? ? ? ? ? ? ? ? ? BlockingQueue workQueue) {
? ? this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
? ? ? ? ?Executors.defaultThreadFactory(), defaultHandler);
}

public ThreadPoolExecutor(int corePoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? int maximumPoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? long keepAliveTime,
? ? ? ? ? ? ? ? ? ? ? ? ? TimeUnit unit,
? ? ? ? ? ? ? ? ? ? ? ? ? BlockingQueue workQueue,
? ? ? ? ? ? ? ? ? ? ? ? ? ThreadFactory threadFactory,
? ? ? ? ? ? ? ? ? ? ? ? ? RejectedExecutionHandler handler) {

? ? if (corePoolSize < 0 ||
? ? ? ? maximumPoolSize <= 0 ||
? ? ? ? maximumPoolSize < corePoolSize ||
? ? ? ? keepAliveTime < 0)
? ? ? ? throw new IllegalArgumentException();
? ? if (workQueue == null || threadFactory == null || handler == null)
? ? ? ? throw new NullPointerException();
? ? this.corePoolSize = corePoolSize;
? ? this.maximumPoolSize = maximumPoolSize;
? ? this.workQueue = workQueue;
? ? this.keepAliveTime = unit.toNanos(keepAliveTime);
? ? this.threadFactory = threadFactory;
? ? this.handler = handler;
}

corePoolSize:線程池核心線程數(shù)量?

如果池中線程數(shù)量少于核心線程池數(shù)量擦俐,則直接新建線程處理當前任務脊阴。

核心線程池空閑不會被回收。

當池中無空閑線程時蚯瞧,新任務將被添加到阻塞隊列

maximumPoolSize:線程池最大線程數(shù)量?

當阻塞隊列已滿嘿期,并且有新任務還在入隊時,創(chuàng)建新的線程處理埋合,直到線程數(shù)大于maximumPoolSize备徐。

超出corePoolSize部分的線程超過空閑時間后會被回收

當線程已經(jīng)超出corePoolSize,并且隊列容量已滿甚颂,則拒絕入隊蜜猾。

keepAliveTime unit:線程存活時間?

當線程超出corePoolSize時生效

線程空余keepAliveTime后,將被回收

workQueue:線程使用阻塞隊列

threadFactory:創(chuàng)建線程池工廠?

用于控制創(chuàng)建線程或者銷毀線程時加入其它邏輯

handler:線程池拒絕策略?

直接丟棄(DiscardPolicy)

丟棄隊列中最老的任務(DiscardOldestPolicy)振诬。

拋異常(AbortPolicy)

將任務分給調用線程來執(zhí)行(CallerRunsPolicy)

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末蹭睡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赶么,更是在濱河造成了極大的恐慌肩豁,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辫呻,死亡現(xiàn)場離奇詭異清钥,居然都是意外死亡,警方通過查閱死者的電腦和手機放闺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門循捺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人雄人,你說我怎么就攤上這事。” “怎么了础钠?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵恰力,是天一觀的道長。 經(jīng)常有香客問我旗吁,道長踩萎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任很钓,我火速辦了婚禮香府,結果婚禮上,老公的妹妹穿的比我還像新娘码倦。我一直安慰自己企孩,他們只是感情好,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布袁稽。 她就那樣靜靜地躺著勿璃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪推汽。 梳的紋絲不亂的頭發(fā)上补疑,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機與錄音歹撒,去河邊找鬼莲组。 笑死,一個胖子當著我的面吹牛暖夭,可吹牛的內容都是我干的锹杈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鳞尔,長吁一口氣:“原來是場噩夢啊……” “哼嬉橙!你這毒婦竟也來了?” 一聲冷哼從身側響起寥假,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤市框,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后糕韧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枫振,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年萤彩,在試婚紗的時候發(fā)現(xiàn)自己被綠了粪滤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡雀扶,死狀恐怖杖小,靈堂內的尸體忽然破棺而出肆汹,到底是詐尸還是另有隱情,我是刑警寧澤予权,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布昂勉,位于F島的核電站,受9級特大地震影響扫腺,放射性物質發(fā)生泄漏岗照。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一笆环、第九天 我趴在偏房一處隱蔽的房頂上張望攒至。 院中可真熱鬧,春花似錦躁劣、人聲如沸迫吐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽渠抹。三九已至,卻和暖如春闪萄,著一層夾襖步出監(jiān)牢的瞬間梧却,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工败去, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留放航,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓圆裕,卻偏偏與公主長得像广鳍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子吓妆,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內容