??????addThread?????У??????????????????????Worker????????????????threadFactory??????????μ????t????????t????????????Worker???????????thread?????????workers.add(w)??Worker?????????????????С?
????????????????Worker???????
????private final class Worker implements Runnable {
????private final ReentrantLock runLock = new ReentrantLock();
????private Runnable firstTask;
????volatile long completedTasks;
????Thread thread;
????Worker(Runnable firstTask) {
????this.firstTask = firstTask;
????}
????boolean isActive() {
????return runLock.isLocked();
????}
????void interruptIfIdle() {
????final ReentrantLock runLock = this.runLock;
????if (runLock.tryLock()) {
????try {
????if (thread != Thread.currentThread())
????thread.interrupt();
????} finally {
????runLock.unlock();
????}
????}
????}
????void interruptNow() {
????thread.interrupt();
????}
????private void runTask(Runnable task) {
????final ReentrantLock runLock = this.runLock;
????runLock.lock();
????try {
????if (runState < STOP &&
????Thread.interrupted() &&
????runState >= STOP)
????boolean ran = false;
????beforeExecute(thread?? task);   //beforeExecute??????ThreadPoolExecutor??????????????о?????????????????
????//???????????????????????afterExecute???????????Щ???????????????????????????          
????try {
????task.run();
????ran = true;
????afterExecute(task?? null);
????++completedTasks;
????} catch (RuntimeException ex) {
????if (!ran)
????afterExecute(task?? ex);
????throw ex;
????}
????} finally {
????runLock.unlock();
????}
????}
????public void run() {
????try {
????Runnable task = firstTask;
????firstTask = null;
????while (task != null || (task = getTask()) != null) {
????runTask(task);
????task = null;
????}
????} finally {
????workerDone(this);   //????????????????????????????????      
????}
????}
????}
????????????????Runnable????????????Thread t = threadFactory.newThread(w);Ч????????????Ч???????????
????Thread t = new Thread(w);
?????????????????Runnable?????????t????????Runnable??
???????Worker?????Runnable??????????????????????run()???????
????public void run() {
????try {
????Runnable task = firstTask;
????firstTask = null;
????while (task != null || (task = getTask()) != null) {
????runTask(task);
????task = null;
????}
????} finally {
????workerDone(this);
????}
????}
??????run?????????????????????????е????????????????????????firstTask???????runTask()?????firstTask?????while??????治?????getTask()???μ?????????У????????????????????????????????????getTask??ThreadPoolExecutor???е????????????Worker???е????????????getTask??????????
????Runnable getTask() {
????for (;;) {
????try {
????int state = runState;
????if (state > SHUTDOWN)
????return null;
????Runnable r;
????if (state == SHUTDOWN)  // Help drain queue
????r = workQueue.poll();
????else if (poolSize > corePoolSize || allowCoreThreadTimeOut) //?????????????????С???????????????????????????
????//?????poll??????????????????????????????null
????r = workQueue.poll(keepAliveTime?? TimeUnit.NANOSECONDS);
????else
????r = workQueue.take();
????if (r != null)
????return r;
????if (workerCanExit()) {    //?????????????r?null?????ж?????worker?????????
????if (runState >= SHUTDOWN) // Wake up others
????interruptIdleWorkers();   //?ж???????????worker
????return null;
????}
????// Else retry
????} catch (InterruptedException ie) {
????// On interruption?? re-check runState
????}
????}
????}
??????getTask?У????ж??????????????runState????SHUTDOWN?????STOP????TERMINATED????????????null??
???????runState?SHUTDOWN????RUNNING???????????????????
?????????????????????????????СcorePoolSize???????????????е??????????д??????????poll(time??timeUnit)????????????????????????????????????????null??
????????ж??????????r????null???null?????????workerCanExit()???????ж???worker???????????????????workerCanExit()??????
????private boolean workerCanExit() {
????final ReentrantLock mainLock = this.mainLock;
????mainLock.lock();
????boolean canExit;
????//???runState???????STOP????????????????????
????//????  ???????????????????д????????????е???????????1
????try {
????canExit = runState >= STOP ||
????workQueue.isEmpty() ||
????(allowCoreThreadTimeOut &&
????poolSize > Math.max(1?? corePoolSize));
????} finally {
????mainLock.unlock();
????}
????return canExit;
????}
??????????????????STOP??????????????????????????????????????????д????????????????1???????worker????????????worker??????????interruptIdleWorkers()?ж???????????worker??????????interruptIdleWorkers()??????
????void interruptIdleWorkers() {
????final ReentrantLock mainLock = this.mainLock;
????mainLock.lock();
????try {
????for (Worker w : workers)  //???????????worker??interruptIfIdle()????
????w.interruptIfIdle();
????} finally {
????mainLock.unlock();
????}
????}
??????????????????????????????worker??interruptIfIdle()????????worker??interruptIfIdle()?????У?
????void interruptIfIdle() {
????final ReentrantLock runLock = this.runLock;
????if (runLock.tryLock()) {    //????????????tryLock()???????????????????worker??????????????????????????????????????
????//?????????????????????worker?????????
????try {
????if (thread != Thread.currentThread()) 
????thread.interrupt();
????} finally {
????runLock.unlock();
????}
????}
????}
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????С???????????????в?????????????????????????????????????????й???????ε????????????????????????????????????????????????????????????С?
???????????addIfUnderMaximumPoolSize??????????????????????????addIfUnderCorePoolSize?????????????????????????????addIfUnderMaximumPoolSize?????????????е???????????????С?????????????????????????????????е??
????private boolean addIfUnderMaximumPoolSize(Runnable firstTask) {
????Thread t = null;
????final ReentrantLock mainLock = this.mainLock;
????mainLock.lock();
????try {
????if (poolSize < maximumPoolSize && runState == RUNNING)
????t = addThread(firstTask);
????} finally {
????mainLock.unlock();
????}
????if (t == null)
????return false;
????t.start();
????return true;
????}
??????????У????????addIfUnderCorePoolSize??????????????????????if????ж??????е?poolSize < maximumPoolSize????????
?????????????????????????????????????????е??????????????????????????????????£?
????1???????????corePoolSize??maximumPoolSize????壻
????2????Σ?????Worker????????????????
????3????????????????????????????????????????????4??
???????????????е???????С??corePoolSize???????????????????????????????????
???????????????е???????>=corePoolSize?????????????????????????????????е??У????????????????????????????????????У????????????????????????????????????????????μ???????????????
???????????????е?????????maximumPoolSize???????????????????д????
????????????е???????????? corePoolSize??????????????????keepAliveTime?????????????????????е?????????????corePoolSize????????????????е??????????????????????е????????????keepAliveTime?????????????
????3.?????е????????
???????????£????????????????????????????????????????????????
??????????????????????????????????????????????????????????????
????prestartCoreThread()??????????????????
????prestartAllCoreThreads()??????????к??????
????????????2????????????
????public boolean prestartCoreThread() {
????return addIfUnderCorePoolSize(null); //?????????????null
????}
????public int prestartAllCoreThreads() {
????int n = 0;
????while (addIfUnderCorePoolSize(null))//?????????????null
????++n;
????return n;
????}
??????????洫??????????null???????2С???????????????????????null??????????????????getTask?????е?
????r = workQueue.take();
????????????????????????
????4.????????м???????
???????????????????????????У???workQueue???????????????е?????
????workQueue???????BlockingQueue<Runnable>???????????????????????
????1??ArrayBlockingQueue????????????????????У?????д?????????????С??
????2??LinkedBlockingQueue????????????????????У???????????????????д?С????????Integer.MAX_VALUE??
????3??synchronousQueue????????б??????????????????????????????????????????????????????