注:關(guān)鍵的代碼的部分會(huì)有注釋。
唉,好記性不如爛筆頭,時(shí)間久了都忘記了,寫(xiě)下來(lái)以后自己忘記時(shí)復(fù)習(xí)所用
構(gòu)造方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> 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; // 線程池保存線程數(shù)量,即使任務(wù)隊(duì)列為空, 新建時(shí)為0
this.maximumPoolSize = maximumPoolSize; //最大工作線程數(shù)量
this.workQueue = workQueue; // 任務(wù)隊(duì)列(注意設(shè)置任務(wù)隊(duì)列大小例如1000 避免任務(wù)隊(duì)列無(wú)限拓展內(nèi)存溢出)
this.keepAliveTime = unit.toNanos(keepAliveTime); //獲取任務(wù)超時(shí)時(shí)間
this.threadFactory = threadFactory; //線程工廠(建議使用自定義線程工廠取特殊名字,若程序運(yùn)行出錯(cuò)jstack工具能快速找到有問(wèn)題得線程)
this.handler = handler; // 任務(wù)隊(duì)列滿之后處理策略(共4種,具體情況具體選擇)
}
狀態(tài)屬性
/**ctl 是工作線程和運(yùn)行狀態(tài)合并 具體可以看此段代碼得上面得注釋*/
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
/***/
private static final int COUNT_BITS = Integer.SIZE - 3;
/***/
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
/**獲取運(yùn)行狀態(tài)*/
private static int runStateOf(int c) { return c & ~CAPACITY; }
/**獲取工作線程數(shù)量*/
private static int workerCountOf(int c) { return c & CAPACITY; }
private static int ctlOf(int rs, int wc) { return rs | wc; }
submit 方法
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
execute方法
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
此處注釋已經(jīng)詳細(xì)描述了這個(gè)3if具體目的,談?wù)剛€(gè)人理解workerCountOf(c) < corePoolSize工作線程小于默認(rèn)工作線程則添加任務(wù),會(huì)創(chuàng)建新工作線程獲取任務(wù)執(zhí)行直到不滿足上述條件。isRunning(c) && workQueue.offer(command)線程池處于正在運(yùn)行狀態(tài)(RUNNING )且向工作隊(duì)列添加任務(wù)成功,重新獲取ctl的值。若此時(shí)線程不為運(yùn)行狀態(tài)(!=RUNNING)那么移除任務(wù),拒絕任務(wù)。若此時(shí)工作線程數(shù)量為0那么添加空任務(wù)重新創(chuàng)建工作線程。!addWorker(command, false) 會(huì)新增工作線程然后直到最大線程maximumPoolSize,當(dāng)然失敗之后則根絕構(gòu)造函數(shù)的拒絕策略決絕任務(wù)即可。
addWorker
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
for (;;) {
int wc = workerCountOf(c);
if (wc >= CAPACITY ||
//若工作線程大于了默認(rèn)線程或者最大線程就會(huì)執(zhí)行execute方法的第二步或者第三步,直接入隊(duì)列
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
if (compareAndIncrementWorkerCount(c))
break retry;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;
// else CAS failed due to workerCount change; retry inner loop
}
}
boolean workerStarted = false;
boolean workerAdded = false;
Worker w = null;
try {
//新建任務(wù)
w = new Worker(firstTask);
final Thread t = w.thread;
if (t != null) {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
// Recheck while holding lock.
// Back out on ThreadFactory failure or if
// shut down before lock acquired.
int rs = runStateOf(ctl.get());
if (rs < SHUTDOWN ||
(rs == SHUTDOWN && firstTask == null)) {
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
//加入工作隊(duì)列
workers.add(w);
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
if (workerAdded) {
//啟動(dòng)工作隊(duì)列
t.start();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
worker的創(chuàng)建以及run方法
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker // 運(yùn)行runWorker時(shí)可中斷
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this); //this使用的work本身
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
//獲取任務(wù)并執(zhí)行
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
//退出工作線程
processWorkerExit(w, completedAbruptly);
}
}
獲取任務(wù)getTask
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
//從工作隊(duì)列中獲取任務(wù),注意這里隊(duì)列使用的poll方法
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
退出工作隊(duì)列processWorkerExit
private void processWorkerExit(Worker w, boolean completedAbruptly) {
if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
decrementWorkerCount();
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
completedTaskCount += w.completedTasks;
workers.remove(w);
} finally {
mainLock.unlock();
}
//不清楚為啥結(jié)束
tryTerminate();
//主要回收工作線程 正常結(jié)束則根據(jù)corePoolSize大小回收 非正常結(jié)束則添加空的任務(wù)
int c = ctl.get();
//檢查是否為RUNNING 或 SHUTDOWN
if (runStateLessThan(c, STOP)) {
if (!completedAbruptly) {
int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
if (min == 0 && ! workQueue.isEmpty())
min = 1;
if (workerCountOf(c) >= min)
return; // replacement not needed
}
addWorker(null, false);
}
}
總結(jié)執(zhí)行流程
線程池拓張過(guò)程(不考慮外部關(guān)閉線程池)
若小于corePoolSize 會(huì)增加工作線程 。若大于corePoolSize 會(huì)直接入隊(duì),成功就直接獲取任務(wù)執(zhí)行,失?。ㄈ蝿?wù)隊(duì)列滿或線程池關(guān)閉,這里指線程任務(wù)隊(duì)列滿)則會(huì)創(chuàng)建新的工作線程。
關(guān)于線程池大小邊界的判斷
addWorker第2個(gè)參數(shù)為true則邊界corePoolSize,false為maximumPoolSize
線程池回收runWorker,processWorkerExit,completedAbruptly
若正常結(jié)束即completedAbruptly=false,那么會(huì)根據(jù)corePoolSize 大小回收線程池 completedAbruptly為true那么新增空的工作線程