public static interface ConcurrentHashMapV8.Spliterator<T> extends Iterator<T>
This interface exports a subset of expected JDK8 functionality.
Sample usage: Here is one (of the several) ways to compute the sum of the values held in a map using the ForkJoin framework. As illustrated here, Spliterators are well suited to designs in which a task repeatedly splits off half its work into forked subtasks until small enough to process directly, and then joins these subtasks. Variants of this style can also be used in completion-based designs.
ConcurrentHashMapV8<String, Long> m = ...
// split as if have 8 * parallelism, for load balance
int n = m.size();
int p = aForkJoinPool.getParallelism() * 8;
int split = (n < p)? n : p;
long sum = aForkJoinPool.invoke(new SumValues(m.valueSpliterator(), split, null));
// ...
static class SumValues extends RecursiveTask<Long> {
final Spliterator<Long> s;
final int split; // split while > 1
final SumValues nextJoin; // records forked subtasks to join
SumValues(Spliterator<Long> s, int depth, SumValues nextJoin) {
this.s = s; this.depth = depth; this.nextJoin = nextJoin;
}
public Long compute() {
long sum = 0;
SumValues subtasks = null; // fork subtasks
for (int s = split >>> 1; s > 0; s >>>= 1)
(subtasks = new SumValues(s.split(), s, subtasks)).fork();
while (s.hasNext()) // directly process remaining elements
sum += s.next();
for (SumValues t = subtasks; t != null; t = t.nextJoin)
sum += t.join(); // collect subtask results
return sum;
}
}
ConcurrentHashMapV8.Spliterator<T> split()
hasNext() reporting false) if this Spliterator cannot be further split.IllegalStateException - if this Spliterator has already
commenced traversing elementsCopyright © 2013 The Skfiy Open Association. All Rights Reserved.