--- jsr166/src/jsr166e/CountedCompleter.java 2012/11/23 17:50:51 1.14
+++ jsr166/src/jsr166e/CountedCompleter.java 2013/02/05 19:54:06 1.29
@@ -83,7 +83,7 @@ package jsr166e;
* subdivided) to each element of an array or collection; especially
* when the operation takes a significantly different amount of time
* to complete for some elements than others, either because of
- * intrinsic variation (for example IO) or auxiliary effects such as
+ * intrinsic variation (for example I/O) or auxiliary effects such as
* garbage collection. Because CountedCompleters provide their own
* continuations, other threads need not block waiting to perform
* them.
@@ -126,14 +126,14 @@ package jsr166e;
* op.apply(array[lo]);
* tryComplete();
* }
- * } }
+ * }}
*
* This design can be improved by noticing that in the recursive case,
* the task has nothing to do after forking its right task, so can
* directly invoke its left task before returning. (This is an analog
* of tail recursion removal.) Also, because the task returns upon
* executing its left task (rather than falling through to invoke
- * tryComplete) the pending count is set to one:
+ * {@code tryComplete}) the pending count is set to one:
*
*
{@code
* class ForEach ...
@@ -183,12 +183,13 @@ package jsr166e;
*
* Searching. A tree of CountedCompleters can search for a
* value or property in different parts of a data structure, and
- * report a result in an {@link java.util.concurrent.AtomicReference}
- * as soon as one is found. The others can poll the result to avoid
- * unnecessary work. (You could additionally {@link #cancel} other
- * tasks, but it is usually simpler and more efficient to just let
- * them notice that the result is set and if so skip further
- * processing.) Illustrating again with an array using full
+ * report a result in an {@link
+ * java.util.concurrent.atomic.AtomicReference AtomicReference} as
+ * soon as one is found. The others can poll the result to avoid
+ * unnecessary work. (You could additionally {@linkplain #cancel
+ * cancel} other tasks, but it is usually simpler and more efficient
+ * to just let them notice that the result is set and if so skip
+ * further processing.) Illustrating again with an array using full
* partitioning (again, in practice, leaf tasks will almost always
* process more than one element):
*
@@ -223,7 +224,7 @@ package jsr166e;
* public static E search(E[] array) {
* return new Searcher(null, array, new AtomicReference(), 0, array.length).invoke();
* }
- *}}
+ * }}
*
* In this example, as well as others in which tasks have no other
* effects except to compareAndSet a common result, the trailing
@@ -277,12 +278,12 @@ package jsr166e;
* }
* public void onCompletion(CountedCompleter> caller) {
* if (caller != this) {
- * MapReducer child = (MapReducer)caller;
- * MapReducer sib = child.sibling;
- * if (sib == null || sib.result == null)
- * result = child.result;
- * else
- * result = reducer.apply(child.result, sib.result);
+ * MapReducer child = (MapReducer)caller;
+ * MapReducer sib = child.sibling;
+ * if (sib == null || sib.result == null)
+ * result = child.result;
+ * else
+ * result = reducer.apply(child.result, sib.result);
* }
* }
* public E getRawResult() { return result; }
@@ -291,7 +292,7 @@ package jsr166e;
* return new MapReducer(null, array, mapper, reducer,
* 0, array.length).invoke();
* }
- * } }
+ * }}
*
* Here, method {@code onCompletion} takes a form common to many
* completion designs that combine results. This callback-style method
@@ -303,7 +304,7 @@ package jsr166e;
* distinguishes cases. Most often, when the caller is {@code this},
* no action is necessary. Otherwise the caller argument can be used
* (usually via a cast) to supply a value (and/or links to other
- * values) to be combined. Asuuming proper use of pending counts, the
+ * values) to be combined. Assuming proper use of pending counts, the
* actions inside {@code onCompletion} occur (once) upon completion of
* a task and its subtasks. No additional synchronization is required
* within this method to ensure thread safety of accesses to fields of
@@ -388,7 +389,7 @@ public abstract class CountedCompleter completer,
@@ -401,7 +402,7 @@ public abstract class CountedCompleter completer) {
this.completer = completer;
@@ -422,7 +423,7 @@ public abstract class CountedCompleter caller) {
}
@@ -448,9 +449,9 @@ public abstract class CountedCompleter caller) {
return true;
@@ -509,7 +510,7 @@ public abstract class CountedCompleter {@code
- * for(CountedCompleter> c = firstComplete(); c != null; c = c.nextComplete()) {
+ * for (CountedCompleter> c = firstComplete();
+ * c != null;
+ * c = c.nextComplete()) {
* // ... process c ...
* }}
*
@@ -659,7 +662,7 @@ public abstract class CountedCompleter a = this, s = a;
@@ -669,7 +672,7 @@ public abstract class CountedCompleter() {
- public sun.misc.Unsafe run() throws Exception {
- java.lang.reflect.Field f = sun.misc
- .Unsafe.class.getDeclaredField("theUnsafe");
- f.setAccessible(true);
- return (sun.misc.Unsafe) f.get(null);
- }});
- } catch (java.security.PrivilegedActionException e) {
- throw new RuntimeException("Could not initialize intrinsics",
- e.getCause());
- }
+ } catch (SecurityException tryReflectionInstead) {}
+ try {
+ return java.security.AccessController.doPrivileged
+ (new java.security.PrivilegedExceptionAction() {
+ public sun.misc.Unsafe run() throws Exception {
+ Class k = sun.misc.Unsafe.class;
+ for (java.lang.reflect.Field f : k.getDeclaredFields()) {
+ f.setAccessible(true);
+ Object x = f.get(null);
+ if (k.isInstance(x))
+ return k.cast(x);
+ }
+ throw new NoSuchFieldError("the Unsafe");
+ }});
+ } catch (java.security.PrivilegedActionException e) {
+ throw new RuntimeException("Could not initialize intrinsics",
+ e.getCause());
}
}
}