??????.??????????????????
?????????????????????????ThreadPoolExecutor????????????????????????????????????????????漸?????潲??
????1.??????
????2.????????
????3.?????е????????
????4.????????м???????
????5.??????????
????6.???????
????7.????????????????
????1.??????
??????ThreadPoolExecutor?ж????????Volatile?????????????????static final???????????????????
????volatile int runState;
????static final int RUNNING    = 0;
????static final int SHUTDOWN   = 1;
????static final int STOP       = 2;
????static final int TERMINATED = 3;
????runState??????????????????????volatile???????????????????????
????????????static final???????runState????????????
????????????????????????????RUNNING????
?????????????shutdown()???????????????SHUTDOWN????????????????????μ??????????????????????????
?????????????shutdownNow()???????????????STOP??????????????????μ????????????????????????е?????
?????????????SHUTDOWN??STOP???????????й????????????????????????????????н???????????????TERMINATED????
????2.????????
?????????????????????????????????????????????????????????ThreadPoolExecutor???????????Щ???????????????
????private final BlockingQueue<Runnable> workQueue;              //????????У????????????е?????
????private final ReentrantLock mainLock = new ReentrantLock();   //?????????????????????????????????С
????//??runState????????????????
????private final HashSet<Worker> workers = new HashSet<Worker>();  //????????????
????private volatile long  keepAliveTime;    //????????  
????private volatile boolean allowCoreThreadTimeOut;   //????????????????????????
????private volatile int   corePoolSize;     //???????С?????????е?????????????????????????????????????????У?
????private volatile int   maximumPoolSize;   //?????????????????
????private volatile int   poolSize;       //?????е?????????
????private volatile RejectedExecutionHandler handler; //??????????
????private volatile ThreadFactory threadFactory;   //???????????????????
????private int largestPoolSize;   //??????????????????????????????
????private long completedTaskCount;   //????????????????????????
??????????????????????????????????????????????corePoolSize??maximumPoolSize??largestPoolSize??????????
????corePoolSize??????????????????С??????????????????????С??????????????
?????????????????????????????10????????????????????????????
???????????10?????????й???????е?????????????????е????????
??????10?????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????4??????????????
???????????????????4?????????????
??????????14??????????????????????????????????????????????????μ?????????????????Щ???????
????????14?????????????????????????????????????????????????????????????4????????????????????10????????????????????????????
????????????е?corePoolSize??10????maximumPoolSize??14??10+4????
????????corePoolSize???????С??maximumPoolSize???????????????????????????????????????????????????
????????????????????????滹???corePoolSize??????????С??
????largestPoolSize??????????????????????????????????????????й???????????????????????????κι????
????????????????????????????????????????????????Щ?????
??????ThreadPoolExecutor???У??????????????????execute()????????????submit????????????????????submit?????????????????execute()???????????????????о?execute()????????????????
????public void execute(Runnable command) {
????if (command == null)
????throw new NullPointerException();
????if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
????if (runState == RUNNING && workQueue.offer(command)) {
????if (runState != RUNNING || poolSize == 0)
????ensureQueuedTaskHandled(command);
????}
????else if (!addIfUnderMaximumPoolSize(command))
????reject(command); // is shutdown or saturated
????}
????}
????????????????????????????????????????????????????
??????????ж?????????command????null??????null????????????????
??????????????????ú??????£?
????if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command))
????????????????????????????????????????????????е?????????С???????С??????????????????if???????
????????????е???????С???????С?????????к??????????У?
????addIfUnderCorePoolSize(command)
????????????addIfUnderCorePoolSize???????????false???????????????if???飬????????????????????????
????????????addIfUnderCorePoolSize???????????false?????????ж??
????if (runState == RUNNING && workQueue.offer(command))
?????????????????RUNNING?????????????????????У????????????????RUNNING??????????????????????????У?
????addIfUnderMaximumPoolSize(command)
??????????addIfUnderMaximumPoolSize?????????????reject()???????????????????
?????????棺
????if (runState == RUNNING && workQueue.offer(command))
??????????У???????????????RUNNING????????????????????г??????????????ж??
????if (runState != RUNNING || poolSize == 0)
????????ж?????????????????????????????е????????????????shutdown????shutdownNow????????????????????????????????????У?
????ensureQueuedTaskHandled(command)
???????????????????????????????? ??????????????е????????????
????????????2???????????????addIfUnderCorePoolSize??addIfUnderMaximumPoolSize??
????private boolean addIfUnderCorePoolSize(Runnable firstTask) {
????Thread t = null;
????final ReentrantLock mainLock = this.mainLock;
????mainLock.lock();
????try {
????if (poolSize < corePoolSize && runState == RUNNING)
????t = addThread(firstTask);        //???????????firstTask????  
????} finally {
????mainLock.unlock();
????}
????if (t == null)
????return false;
????t.start();
????return true;
????}
?????????addIfUnderCorePoolSize????????????????????????????????????????????С???е?????????濴?????????????????????????????漰??????????仯???????if????ж????????е??????????С???????С????????????????????????execute()?????в???????ж?????????????????????С???????С??????addIfUnderCorePoolSize??????????????????????ж??????????????ж?????в???м?????????????execute?????ж?????poolSizeС??corePoolSize?????ж????????????????????????????????????????poolSize??С??corePoolSize???????????????????????ж?????????ж????????????RUNNING???????????????п?????????????е?????shutdown????shutdownNow??????????????
????t = addThread(firstTask);
?????????????????????????????????????????????Thread??????????????????ж?t????????????????????????????poolSize>=corePoolSize????runState??????RUNNING???????????t.start()???????????
???????????????addThread??????????
????private Thread addThread(Runnable firstTask) {
????Worker w = new Worker(firstTask);
????Thread t = threadFactory.newThread(w);  //??????????????????  
????if (t != null) {
????w.thread = t;            //???????????????????w????????      
????workers.add(w);
????int nt = ++poolSize;     //??????????1      
????if (nt > largestPoolSize)
????largestPoolSize = nt;
????}
????return t;
????}