????????????????????????????????????????????????????????????????????д???????????????????????????????
???????У?addWorker?????????????????1??????????????????????????2?????????????????????????????????????????????????????????
??????????????????
????????????Worker?????????????????????£?
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock();
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
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);
}
}
???????????????????????????? getTask() ???????????У?????????????????null??????????
??????getTask??????£?
private Runnable getTask() {
boolean timedOut = false;
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime?? TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
????getTask????????null????????????????????????????????????????
?????????????????????null??
??????????????????????????null??1??????maximumPoolSize??2??????corePoolSize????????????keepAliveTime??3????????allowCoreThreadTimeOut???????????????keepAliveTime?????????????????????????????????????飬????????????????????????????з?????????λ???????????????????????????
???????workQueue.poll()????workQueue.take()???????????????????????????????????????м????????????????????????????????????????????corePoolSize??????????allowCoreThreadTimeOut??
???????
?????????????ThreadPoolExecutor????ò????????????????????????????????????????????:)