13 |
|
import java.util.List; |
14 |
|
import java.util.concurrent.AbstractExecutorService; |
15 |
|
import java.util.concurrent.Callable; |
16 |
– |
import java.util.concurrent.CountDownLatch; |
16 |
|
import java.util.concurrent.ExecutorService; |
17 |
|
import java.util.concurrent.Future; |
18 |
|
import java.util.concurrent.RejectedExecutionException; |
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 |
799 |
< |
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 || |
805 |
< |
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) { |
877 |
< |
decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL); |
878 |
< |
tryTerminate(false); // handle failure during shutdown |
879 |
< |
} |
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) { |
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 |