--- jsr166/src/jsr166y/ForkJoinTask.java 2011/02/22 00:39:31 1.74 +++ jsr166/src/jsr166y/ForkJoinTask.java 2011/07/01 18:30:14 1.80 @@ -1,24 +1,20 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ package jsr166y; import java.io.Serializable; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.RandomAccess; -import java.util.Map; import java.lang.ref.WeakReference; import java.lang.ref.ReferenceQueue; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.RunnableFuture; @@ -165,12 +161,14 @@ public abstract class ForkJoinTask im * See the internal documentation of class ForkJoinPool for a * general implementation overview. ForkJoinTasks are mainly * responsible for maintaining their "status" field amidst relays - * to methods in ForkJoinWorkerThread and ForkJoinPool. The - * methods of this class are more-or-less layered into (1) basic - * status maintenance (2) execution and awaiting completion (3) - * user-level methods that additionally report results. This is - * sometimes hard to see because this file orders exported methods - * in a way that flows well in javadocs. + * to methods in ForkJoinWorkerThread and ForkJoinPool. + * + * The methods of this class are more-or-less layered into + * (1) basic status maintenance + * (2) execution and awaiting completion + * (3) user-level methods that additionally report results. + * This is sometimes hard to see because this file orders exported + * methods in a way that flows well in javadocs. */ /* @@ -282,8 +280,11 @@ public abstract class ForkJoinTask im if (s == 0) UNSAFE.compareAndSwapInt(this, statusOffset, 0, SIGNAL); - else + else { wait(millis); + if (millis > 0L) + break; + } } } } @@ -381,13 +382,13 @@ public abstract class ForkJoinTask im * periods. However, since we do not know when the last joiner * completes, we must use weak references and expunge them. We do * so on each operation (hence full locking). Also, some thread in - * any ForkJoinPool will call helpExpunge when its pool becomes - * isQuiescent. + * any ForkJoinPool will call helpExpungeStaleExceptions when its + * pool becomes isQuiescent. */ static final class ExceptionNode extends WeakReference>{ final Throwable ex; ExceptionNode next; - final long thrower; + final long thrower; // use id not ref to avoid weak cycles ExceptionNode(ForkJoinTask task, Throwable ex, ExceptionNode next) { super(task, exceptionTableRefQueue); this.ex = ex; @@ -403,7 +404,7 @@ public abstract class ForkJoinTask im */ private int setExceptionalCompletion(Throwable ex) { int h = System.identityHashCode(this); - ReentrantLock lock = exceptionTableLock; + final ReentrantLock lock = exceptionTableLock; lock.lock(); try { expungeStaleExceptions(); @@ -428,7 +429,7 @@ public abstract class ForkJoinTask im */ private void clearExceptionalCompletion() { int h = System.identityHashCode(this); - ReentrantLock lock = exceptionTableLock; + final ReentrantLock lock = exceptionTableLock; lock.lock(); try { ExceptionNode[] t = exceptionTable; @@ -473,7 +474,7 @@ public abstract class ForkJoinTask im return null; int h = System.identityHashCode(this); ExceptionNode e; - ReentrantLock lock = exceptionTableLock; + final ReentrantLock lock = exceptionTableLock; lock.lock(); try { expungeStaleExceptions(); @@ -488,7 +489,7 @@ public abstract class ForkJoinTask im if (e == null || (ex = e.ex) == null) return null; if (e.thrower != Thread.currentThread().getId()) { - Class ec = ex.getClass(); + Class ec = ex.getClass(); try { Constructor noArgCtor = null; Constructor[] cs = ec.getConstructors();// public ctors only @@ -539,11 +540,11 @@ public abstract class ForkJoinTask im } /** - * If lock is available, poll any stale refs and remove them. + * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { - ReentrantLock lock = exceptionTableLock; + final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); @@ -694,7 +695,7 @@ public abstract class ForkJoinTask im if (t != null) { if (ex != null) t.cancel(false); - else if (t.doJoin() < NORMAL && ex == null) + else if (t.doJoin() < NORMAL) ex = t.getException(); } } @@ -751,7 +752,7 @@ public abstract class ForkJoinTask im if (t != null) { if (ex != null) t.cancel(false); - else if (t.doJoin() < NORMAL && ex == null) + else if (t.doJoin() < NORMAL) ex = t.getException(); } }