ThreadPoolExecutor執行緒池原始碼解讀

2021-07-14 08:10:35 字數 1176 閱讀 1386

主要變數:

private volatile int   corepoolsize;

private volatile int maximumpoolsize;

private volatile int poolsize;

建構函式:也就是建立類的時候,需要注入引數。

public threadpoolexecutor(int corepoolsize,

int maximumpoolsize,

long keepalivetime,

timeunit unit,

blockingqueueworkqueue,

threadfactory threadfactory,

rejectedexecutionhandler handler)

執行函式:執行在未來給定任務的某個時候。任務在乙個新的執行緒或在現有池執行緒可以執行。

public void execute(runnable command) 

else if (!addifundermaximumpoolsize(command))

reject(command); // is shutdown or saturated

}}

ensurequeuedtaskhandled()函式:使用了鎖機制。

private void ensurequeuedtaskhandled(runnable command)  finally 

if (reject)

reject(command);

else if (t != null)

t.start();

}

reject 函式

void reject(runnable command)

rejectedexecution函式 執行任務中的r呼叫者的執行緒,如果執行程式已被關閉,在這種情況下,任務被丟棄。

public void rejectedexecution(runnable r, threadpoolexecutor e) 

}

mysql 執行緒池原始碼 執行緒池原始碼解析

1.前言 我個人覺得理論性的東西可能大家都懂,但是具體的實現細節可能並不是很清楚所以才想記錄一下,加深記憶。2.關鍵原始碼解析 1 ctl private final atomicinteger ctl new atomicinteger ctlof running,0 private static...

原始碼學習 執行緒池原始碼自學篇

執行緒池作為專案中經常用到的類,也在面試中備受青睞,個人對於原始碼也只是讀過一些,很多知識點都是一知半解,藉此機會自己再溫故一下。我是在邊自學邊寫這些東西,可能語言上或者邏輯上不太完善,請大家不要介意。執行緒池是什麼?官網解釋 執行緒池主要解決兩個問題 在需要執行大量執行緒的場景,減少建立每個執行緒...

執行緒池原始碼閱讀(二)

僅大致過了下,有問題的請指出,謝謝。1.8通過乙個樣本場景了解新增任務流程。執行緒池配置 任務 輸出1,睡眠300s,輸出2 執行 新增9個任務至執行緒池 submit 提交任務使用submit 方法,如下 關鍵方法execute public future submit runnable task...