|
ehcache | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public static interface ConcurrentHashMap.Spliterator<T>
A partitionable iterator. A Spliterator can be traversed directly, but can also be partitioned (before traversal) by creating another Spliterator that covers a non-overlapping portion of the elements, and so may be amenable to parallel execution.
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.
ConcurrentHashMap<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;
}
}
Method Summary | |
---|---|
ConcurrentHashMap.Spliterator<T> |
split()
Returns a Spliterator covering approximately half of the elements, guaranteed not to overlap with those subsequently returned by this Spliterator. |
Methods inherited from interface java.util.Iterator |
---|
hasNext, next, remove |
Method Detail |
---|
ConcurrentHashMap.Spliterator<T> split()
hasNext()
reporting false
) if this Spliterator cannot be further split.
IllegalStateException
- if this Spliterator has
already commenced traversing elements
|
ehcache | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |