999 |
|
* 4. This worker timed out waiting for a task, and timed-out |
1000 |
|
* workers are subject to termination (that is, |
1001 |
|
* {@code allowCoreThreadTimeOut || workerCount > corePoolSize}) |
1002 |
< |
* both before and after the timed wait. |
1002 |
> |
* both before and after the timed wait, and if the queue is |
1003 |
> |
* non-empty, this worker is not the last thread in the pool. |
1004 |
|
* |
1005 |
|
* @return task, or null if the worker must exit, in which case |
1006 |
|
* workerCount is decremented |
1008 |
|
private Runnable getTask() { |
1009 |
|
boolean timedOut = false; // Did the last poll() time out? |
1010 |
|
|
1010 |
– |
retry: |
1011 |
|
for (;;) { |
1012 |
|
int c = ctl.get(); |
1013 |
|
int rs = runStateOf(c); |
1018 |
|
return null; |
1019 |
|
} |
1020 |
|
|
1021 |
< |
boolean timed; // Are workers subject to culling? |
1021 |
> |
int wc = workerCountOf(c); |
1022 |
|
|
1023 |
< |
for (;;) { |
1024 |
< |
int wc = workerCountOf(c); |
1025 |
< |
timed = allowCoreThreadTimeOut || wc > corePoolSize; |
1023 |
> |
// Are workers subject to culling? |
1024 |
> |
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; |
1025 |
|
|
1026 |
< |
if (wc <= maximumPoolSize && ! (timedOut && timed)) |
1027 |
< |
break; |
1026 |
> |
if ((wc > maximumPoolSize || (timed && timedOut)) |
1027 |
> |
&& (wc > 1 || workQueue.isEmpty())) { |
1028 |
|
if (compareAndDecrementWorkerCount(c)) |
1029 |
|
return null; |
1030 |
< |
c = ctl.get(); // Re-read ctl |
1032 |
< |
if (runStateOf(c) != rs) |
1033 |
< |
continue retry; |
1034 |
< |
// else CAS failed due to workerCount change; retry inner loop |
1030 |
> |
continue; |
1031 |
|
} |
1032 |
|
|
1033 |
|
try { |