public static interface ConcurrentHashMap8.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;
}
}
Modifier and Type | Method and Description |
---|---|
ConcurrentHashMap8.Spliterator<T> |
split()
Returns a Spliterator covering approximately half of the
elements, guaranteed not to overlap with those subsequently
returned by this Spliterator.
|
ConcurrentHashMap8.Spliterator<T> split()
hasNext()
reporting false
) if this Spliterator cannot be further split.IllegalStateException
- if this Spliterator has
already commenced traversing elements
Follow @ApacheIgnite
Ignite Fabric : ver. 2.0.0 Release Date : April 30 2017