396 |
|
|
397 |
|
/** |
398 |
|
* Create and start a new thread running firstTask as its first |
399 |
< |
* task, only if less than corePoolSize threads are running. |
399 |
> |
* task, only if fewer than corePoolSize threads are running. |
400 |
|
* @param firstTask the task the new thread should run first (or |
401 |
|
* null if none) |
402 |
|
* @return true if successful. |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* Create and start a new thread only if less than maximumPoolSize |
421 |
> |
* Create and start a new thread only if fewer than maximumPoolSize |
422 |
|
* threads are running. The new thread runs as its first task the |
423 |
|
* next task in queue, or if there is none, the given task. |
424 |
|
* @param firstTask the task the new thread should run first (or |
487 |
|
// Wait for one, re-checking state upon interruption |
488 |
|
try { |
489 |
|
return workQueue.take(); |
490 |
< |
} |
491 |
< |
catch(InterruptedException ignore) { |
492 |
< |
} |
490 |
> |
} catch(InterruptedException ignore) {} |
491 |
|
break; |
492 |
|
} |
493 |
|
|
662 |
|
*/ |
663 |
|
public void run() { |
664 |
|
try { |
665 |
< |
for (;;) { |
666 |
< |
Runnable task; |
667 |
< |
if (firstTask != null) { |
670 |
< |
task = firstTask; |
671 |
< |
firstTask = null; |
672 |
< |
} else { |
673 |
< |
task = getTask(); |
674 |
< |
if (task == null) |
675 |
< |
break; |
676 |
< |
} |
665 |
> |
Runnable task = firstTask; |
666 |
> |
firstTask = null; |
667 |
> |
while (task != null || (task = getTask()) != null) { |
668 |
|
runTask(task); |
669 |
|
task = null; // unnecessary but can help GC |
670 |
|
} |
740 |
|
TimeUnit unit, |
741 |
|
BlockingQueue<Runnable> workQueue, |
742 |
|
ThreadFactory threadFactory) { |
752 |
– |
|
743 |
|
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, |
744 |
|
threadFactory, defaultHandler); |
745 |
|
} |
875 |
|
mainLock.lock(); |
876 |
|
try { |
877 |
|
if (workers.size() > 0) { |
878 |
< |
// Check if caller can modify worker threads. |
879 |
< |
// This might not be true even if passed above check, |
880 |
< |
// if the SecurityManager treats some threads specially. |
878 |
> |
// Check if caller can modify worker threads. This |
879 |
> |
// might not be true even if passed above check, if |
880 |
> |
// the SecurityManager treats some threads specially. |
881 |
|
if (security != null) { |
882 |
|
for (Worker w: workers) |
883 |
|
security.checkAccess(w.thread); |
891 |
|
for (Worker w: workers) |
892 |
|
w.interruptIfIdle(); |
893 |
|
} catch(SecurityException se) { |
894 |
< |
// If SecurityManager allows above checks, but then |
895 |
< |
// unexpectedly throws exception when interrupting |
896 |
< |
// threads (which it ought not do), back out as |
897 |
< |
// cleanly as we can. -Some threads may have been |
898 |
< |
// killed but we remain in non-shutdown state. |
894 |
> |
// If SecurityManager allows above checks, but |
895 |
> |
// then unexpectedly throws exception when |
896 |
> |
// interrupting threads (which it ought not do), |
897 |
> |
// back out as cleanly as we can. Some threads may |
898 |
> |
// have been killed but we remain in non-shutdown |
899 |
> |
// state. |
900 |
|
runState = state; |
901 |
|
throw se; |
902 |
|
} |
951 |
|
} |
952 |
|
if (fullyTerminated) |
953 |
|
terminated(); |
963 |
– |
|
954 |
|
return Arrays.asList(workQueue.toArray(EMPTY_RUNNABLE_ARRAY)); |
955 |
|
} |
956 |
|
|
978 |
|
|
979 |
|
public boolean awaitTermination(long timeout, TimeUnit unit) |
980 |
|
throws InterruptedException { |
981 |
+ |
long nanos = unit.toNanos(timeout); |
982 |
|
final ReentrantLock mainLock = this.mainLock; |
983 |
|
mainLock.lock(); |
984 |
|
try { |
994 |
– |
long nanos = unit.toNanos(timeout); |
985 |
|
for (;;) { |
986 |
|
if (runState == TERMINATED) |
987 |
|
return true; |
1092 |
|
* However, this method may fail to remove tasks in |
1093 |
|
* the presence of interference by other threads. |
1094 |
|
*/ |
1105 |
– |
|
1095 |
|
public void purge() { |
1096 |
|
// Fail if we encounter interference during traversal |
1097 |
|
try { |
1394 |
|
* is discarded. |
1395 |
|
*/ |
1396 |
|
public static class CallerRunsPolicy implements RejectedExecutionHandler { |
1408 |
– |
|
1397 |
|
/** |
1398 |
|
* Creates a <tt>CallerRunsPolicy</tt>. |
1399 |
|
*/ |
1417 |
|
* <tt>RejectedExecutionException</tt>. |
1418 |
|
*/ |
1419 |
|
public static class AbortPolicy implements RejectedExecutionHandler { |
1432 |
– |
|
1420 |
|
/** |
1421 |
|
* Creates an <tt>AbortPolicy</tt>. |
1422 |
|
*/ |
1438 |
|
* rejected task. |
1439 |
|
*/ |
1440 |
|
public static class DiscardPolicy implements RejectedExecutionHandler { |
1454 |
– |
|
1441 |
|
/** |
1442 |
|
* Creates <tt>DiscardPolicy</tt>. |
1443 |
|
*/ |