6 |
|
|
7 |
|
package jsr166y; |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import java.util.ArrayList; |
10 |
|
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
13 |
|
import java.util.List; |
14 |
+ |
import java.util.concurrent.AbstractExecutorService; |
15 |
+ |
import java.util.concurrent.Callable; |
16 |
+ |
import java.util.concurrent.ExecutorService; |
17 |
+ |
import java.util.concurrent.Future; |
18 |
+ |
import java.util.concurrent.RejectedExecutionException; |
19 |
+ |
import java.util.concurrent.RunnableFuture; |
20 |
+ |
import java.util.concurrent.TimeUnit; |
21 |
+ |
import java.util.concurrent.TimeoutException; |
22 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
23 |
|
import java.util.concurrent.locks.LockSupport; |
24 |
|
import java.util.concurrent.locks.ReentrantLock; |
17 |
– |
import java.util.concurrent.atomic.AtomicInteger; |
18 |
– |
import java.util.concurrent.CountDownLatch; |
25 |
|
|
26 |
|
/** |
27 |
|
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
435 |
|
|
436 |
|
/** |
437 |
|
* The wakeup interval (in nanoseconds) for the oldest worker |
438 |
< |
* waiting for an event invokes tryShutdownUnusedWorker to shrink |
439 |
< |
* the number of workers. The exact value does not matter too |
440 |
< |
* much, but should be long enough to slowly release resources |
441 |
< |
* during long periods without use without disrupting normal use. |
438 |
> |
* waiting for an event to invoke tryShutdownUnusedWorker to |
439 |
> |
* shrink the number of workers. The exact value does not matter |
440 |
> |
* too much. It must be short enough to release resources during |
441 |
> |
* sustained periods of idleness, but not so short that threads |
442 |
> |
* are continually re-created. |
443 |
|
*/ |
444 |
|
private static final long SHRINK_RATE_NANOS = |
445 |
|
30L * 1000L * 1000L * 1000L; // 2 per minute |
522 |
|
* Lifecycle control. The low word contains the number of workers |
523 |
|
* that are (probably) executing tasks. This value is atomically |
524 |
|
* incremented before a worker gets a task to run, and decremented |
525 |
< |
* when worker has no tasks and cannot find any. Bits 16-18 |
525 |
> |
* when a worker has no tasks and cannot find any. Bits 16-18 |
526 |
|
* contain runLevel value. When all are zero, the pool is |
527 |
|
* running. Level transitions are monotonic (running -> shutdown |
528 |
|
* -> terminating -> terminated) so each transition adds a bit. |
611 |
|
* (rarely) necessary when other count updates lag. |
612 |
|
* |
613 |
|
* @param dr -- either zero or ONE_RUNNING |
614 |
< |
* @param dt == either zero or ONE_TOTAL |
614 |
> |
* @param dt -- either zero or ONE_TOTAL |
615 |
|
*/ |
616 |
|
private void decrementWorkerCounts(int dr, int dt) { |
617 |
|
for (;;) { |
793 |
|
(workerCounts & RUNNING_COUNT_MASK) <= 1); |
794 |
|
long startTime = untimed? 0 : System.nanoTime(); |
795 |
|
Thread.interrupted(); // clear/ignore interrupt |
796 |
< |
if (eventCount != ec || w.runState != 0 || |
797 |
< |
runState >= TERMINATING) // recheck after clear |
791 |
< |
break; |
796 |
> |
if (eventCount != ec || w.isTerminating()) |
797 |
> |
break; // recheck after clear |
798 |
|
if (untimed) |
799 |
|
LockSupport.park(w); |
800 |
|
else { |
801 |
|
LockSupport.parkNanos(w, SHRINK_RATE_NANOS); |
802 |
< |
if (eventCount != ec || w.runState != 0 || |
797 |
< |
runState >= TERMINATING) |
802 |
> |
if (eventCount != ec || w.isTerminating()) |
803 |
|
break; |
804 |
|
if (System.nanoTime() - startTime >= SHRINK_RATE_NANOS) |
805 |
|
tryShutdownUnusedWorker(ec); |
867 |
|
UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc, |
868 |
|
wc + (ONE_RUNNING|ONE_TOTAL))) { |
869 |
|
ForkJoinWorkerThread w = null; |
870 |
+ |
Throwable fail = null; |
871 |
|
try { |
872 |
|
w = factory.newThread(this); |
873 |
< |
} finally { // adjust on null or exceptional factory return |
874 |
< |
if (w == null) { |
869 |
< |
decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL); |
870 |
< |
tryTerminate(false); // handle failure during shutdown |
871 |
< |
} |
873 |
> |
} catch (Throwable ex) { |
874 |
> |
fail = ex; |
875 |
|
} |
876 |
< |
if (w == null) |
876 |
> |
if (w == null) { // null or exceptional factory return |
877 |
> |
decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL); |
878 |
> |
tryTerminate(false); // handle failure during shutdown |
879 |
> |
// If originating from an external caller, |
880 |
> |
// propagate exception, else ignore |
881 |
> |
if (fail != null && runState < TERMINATING && |
882 |
> |
!(Thread.currentThread() instanceof |
883 |
> |
ForkJoinWorkerThread)) |
884 |
> |
UNSAFE.throwException(fail); |
885 |
|
break; |
886 |
+ |
} |
887 |
|
w.start(recordWorker(w), ueh); |
888 |
|
if ((workerCounts >>> TOTAL_COUNT_SHIFT) >= pc) { |
889 |
|
int c; // advance event count |
972 |
|
boolean active = w.active; |
973 |
|
boolean inactivate = false; |
974 |
|
int pc = parallelism; |
975 |
< |
int rs; |
976 |
< |
while (w.runState == 0 && (rs = runState) < TERMINATING) { |
975 |
> |
while (w.runState == 0) { |
976 |
> |
int rs = runState; |
977 |
> |
if (rs >= TERMINATING) { // propagate shutdown |
978 |
> |
w.shutdown(); |
979 |
> |
break; |
980 |
> |
} |
981 |
|
if ((inactivate || (active && (rs & ACTIVE_COUNT_MASK) >= pc)) && |
982 |
|
UNSAFE.compareAndSwapInt(this, runStateOffset, rs, rs - 1)) |
983 |
|
inactivate = active = w.active = false; |
1105 |
|
return true; |
1106 |
|
} |
1107 |
|
|
1108 |
+ |
|
1109 |
|
/** |
1110 |
|
* Actions on transition to TERMINATING |
1111 |
|
* |
1129 |
|
if (passes > 0 && !w.isTerminated()) { |
1130 |
|
w.cancelTasks(); |
1131 |
|
LockSupport.unpark(w); |
1132 |
< |
if (passes > 1) { |
1132 |
> |
if (passes > 1 && !w.isInterrupted()) { |
1133 |
|
try { |
1134 |
|
w.interrupt(); |
1135 |
|
} catch (SecurityException ignore) { |
1458 |
|
|
1459 |
|
/** |
1460 |
|
* Returns the number of worker threads that have started but not |
1461 |
< |
* yet terminated. This result returned by this method may differ |
1461 |
> |
* yet terminated. The result returned by this method may differ |
1462 |
|
* from {@link #getParallelism} when threads are created to |
1463 |
|
* maintain parallelism when others are cooperatively blocked. |
1464 |
|
* |
1706 |
|
} |
1707 |
|
|
1708 |
|
/** |
1709 |
+ |
* Returns true if terminating or terminated. Used by ForkJoinWorkerThread. |
1710 |
+ |
*/ |
1711 |
+ |
final boolean isAtLeastTerminating() { |
1712 |
+ |
return runState >= TERMINATING; |
1713 |
+ |
} |
1714 |
+ |
|
1715 |
+ |
/** |
1716 |
|
* Returns {@code true} if this pool has been shut down. |
1717 |
|
* |
1718 |
|
* @return {@code true} if this pool has been shut down |
1866 |
|
private static final long eventCountOffset = |
1867 |
|
objectFieldOffset("eventCount", ForkJoinPool.class); |
1868 |
|
private static final long eventWaitersOffset = |
1869 |
< |
objectFieldOffset("eventWaiters",ForkJoinPool.class); |
1869 |
> |
objectFieldOffset("eventWaiters", ForkJoinPool.class); |
1870 |
|
private static final long stealCountOffset = |
1871 |
< |
objectFieldOffset("stealCount",ForkJoinPool.class); |
1871 |
> |
objectFieldOffset("stealCount", ForkJoinPool.class); |
1872 |
|
private static final long spareWaitersOffset = |
1873 |
< |
objectFieldOffset("spareWaiters",ForkJoinPool.class); |
1873 |
> |
objectFieldOffset("spareWaiters", ForkJoinPool.class); |
1874 |
|
|
1875 |
|
private static long objectFieldOffset(String field, Class<?> klazz) { |
1876 |
|
try { |