Package net.sf.ehcache.util.concurrent
Interface ConcurrentHashMap.Spliterator<T>
-
- All Superinterfaces:
java.util.Iterator<T>
- Enclosing class:
- ConcurrentHashMap<K,V>
public static interface ConcurrentHashMap.Spliterator<T> extends java.util.Iterator<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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ConcurrentHashMap.Spliterator<T>split()Returns a Spliterator covering approximately half of the elements, guaranteed not to overlap with those subsequently returned by this Spliterator.
-
-
-
Method Detail
-
split
ConcurrentHashMap.Spliterator<T> split()
Returns a Spliterator covering approximately half of the elements, guaranteed not to overlap with those subsequently returned by this Spliterator. After invoking this method, the current Spliterator will not produce any of the elements of the returned Spliterator, but the two Spliterators together will produce all of the elements that would have been produced by this Spliterator had this method not been called. The exact number of elements produced by the returned Spliterator is not guaranteed, and may be zero (i.e., withhasNext()reportingfalse) if this Spliterator cannot be further split.- Returns:
- a Spliterator covering approximately half of the elements
- Throws:
java.lang.IllegalStateException- if this Spliterator has already commenced traversing elements
-
-