Class ParallelCollectionExtensions
Collection.
These methods use Pool.current() to obtain the current pool
(typically set by ParallelScope.withPool(int, java.util.function.Function<groovy.concurrent.AsyncScope, T>)).
If no pool is current, they use ForkJoinPool.commonPool().
A fork-join pool (Pool.fixed(int), Pool.cpu(),
ParallelScope.withPool(int, Function), or the common pool)
runs a Java parallel stream on that pool. Parallelism is a hard cap:
callbacks are not wrapped in managedBlock, so CPU-bound work
does not create spare workers, and the common pool is not enlarged.
A blocking callback can stall that pool when every worker blocks while
a sibling task is still queued. Size a fork-join pool to at least one
more thread than the number of concurrent blockers, or use a pool that
is not a fork-join pool for waiting work.
Pool.virtual(), and Pool.io() when virtual threads are
available, are not fork-join pools. The operation then submits one task
per element to that pool. Blocking callbacks stay on the caller's pool
instead of falling back to the common pool. Before virtual threads,
Pool.io() is a fixed fork-join pool and has the same cap as
Pool.fixed(int). CPU-bound work should stay on Pool.fixed(int)
or Pool.cpu().
A nested operation stays on the same pool. A worker created for a pool
carries that pool, and Pool.current() reports it when the thread
has no explicit binding, so a callback does not rebind the pool on each
element. On a bounded executor that does not create extra threads, the
nested operation runs on the worker already executing the outer callback.
That worker would otherwise wait for a task queued behind it.
Pool.virtual() still submits one task per element.
On the executor path, a short-circuit (findParallel,
findAnyParallel, anyParallel, everyParallel) or a
sibling failure interrupts callbacks that are still running. Interrupting
a thread blocked on an interruptible channel closes that channel. A
fork-join pool does not interrupt a running callback.
A matching null element, a null reduction, and a null group key are
preserved on a fork-join pool and on an executor pool. An empty
search or reduction returns null. maxParallel skips null
elements, matching sequential max(Comparator). minParallel
passes nulls to the comparator, matching sequential min(Comparator).
Inspired by GPars' GParsPoolUtil category methods.
- Since:
- 6.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleananyParallel(Collection<T> self) Returnstrueif any element is true according to Groovy truth, as sequentialany()does.static <T> booleananyParallel(Collection<T> self, Predicate<T> predicate) Returnstrueif any element matches the predicate.static <T,E> List<T> collectManyParallel(Collection<E> self) Flattens a collection of collections into a list, in parallel, as sequentialcollectMany()does.static <T,R> List<R> collectManyParallel(Collection<T> self, Function<T, ? extends Collection<R>> transform) Transforms each element into a collection and flattens the results in parallel (parallel flatMap).static <T,R> List<R> collectParallel(Collection<T> self, Function<T, R> transform) Transforms each element in parallel, returning a new list.static <T> longcountParallel(Collection<T> self, Predicate<T> predicate) Counts elements matching the predicate.static <T> voideachParallel(Collection<T> self, Consumer<T> action) Iterates over the collection in parallel, applying the action to each element.static <T> voideachWithIndexParallel(Collection<T> self, BiConsumer<T, Integer> action) Iterates over the collection in parallel with element indices.static <T> booleaneveryParallel(Collection<T> self) Returnstrueif every element is true according to Groovy truth, as sequentialevery()does.static <T> booleaneveryParallel(Collection<T> self, Predicate<T> predicate) Returnstrueif all elements match the predicate.static <T> List<T>findAllParallel(Collection<T> self) Returns the elements that are true according to Groovy truth, in parallel, as sequentialfindAll()does.static <T> List<T>findAllParallel(Collection<T> self, Predicate<T> filter) Filters the collection in parallel, returning elements that match.static <T> TfindAnyParallel(Collection<T> self, Predicate<T> filter) Finds any element matching the predicate.static <T> TfindParallel(Collection<T> self) Finds the first element that is true according to Groovy truth, in encounter order, as sequentialfind()does.static <T> TfindParallel(Collection<T> self, Predicate<T> filter) Finds the first element matching the predicate in encounter order.static <T> List<T>grepParallel(Collection<T> self) Returns the elements that are true according to Groovy truth, in parallel, as sequentialgrep()does.static <T> List<T>grepParallel(Collection<T> self, Object filter) Filters elements using Groovy'sisCasepattern matching in parallel.groupByParallel(Collection<T> self, Function<T, K> classifier) Groups elements by the classifier function in parallel.static <T> TinjectParallel(Collection<T> self, T seed, BinaryOperator<T> accumulator) Reduces the collection in parallel with an identity value.static <T> TmaxParallel(Collection<T> self) Finds the maximum element by natural ordering, as sequentialmax()does: null elements are skipped, elements compare as Groovy's>does (so numbers of different types compare by value), and the earliest of equal elements is returned.static <T> TmaxParallel(Collection<T> self, Comparator<T> comparator) Finds the maximum element using the given comparator.static <T> TminParallel(Collection<T> self) Finds the minimum element by natural ordering, as sequentialmin()does: null elements are skipped, elements compare as Groovy's<does (so numbers of different types compare by value), and the earliest of equal elements is returned.static <T> TminParallel(Collection<T> self, Comparator<T> comparator) Finds the minimum element using the given comparator.splitParallel(Collection<T> self, Predicate<T> predicate) Partitions the collection into two lists: elements that match the predicate and elements that don't.static ObjectsumParallel(Collection<?> self) Adds the elements together in parallel, as sequentialsum()does: each addition callspluson the left operand.static <T> TsumParallel(Collection<T> self, BinaryOperator<T> accumulator) Reduces the collection in parallel using the given operator.
-
Constructor Details
-
ParallelCollectionExtensions
public ParallelCollectionExtensions()
-
-
Method Details
-
eachParallel
Iterates over the collection in parallel, applying the action to each element. -
collectParallel
Transforms each element in parallel, returning a new list. -
collectManyParallel
Flattens a collection of collections into a list, in parallel, as sequentialcollectMany()does. Outer encounter order is preserved, as is the order inside each element collection. An element that is not a collection fails as it does sequentially.assert [[1, 2], [3], []].collectManyParallel() == [1, 2, 3]
- Since:
- 6.0.1
-
findAllParallel
Filters the collection in parallel, returning elements that match. -
findAllParallel
Returns the elements that are true according to Groovy truth, in parallel, as sequentialfindAll()does. Encounter order is preserved.assert [1, 0, null, 'a', '', [2], []].findAllParallel() == [1, 'a', [2]]
- Since:
- 6.0.1
-
findParallel
Finds the first element matching the predicate in encounter order. Although evaluation happens in parallel, the result is the matching element with the lowest index. A matchingnulland no match both returnnull. UsefindAnyParallel(java.util.Collection<T>, java.util.function.Predicate<T>)if any match will do — it may be faster as it avoids ordering constraints. -
findParallel
Finds the first element that is true according to Groovy truth, in encounter order, as sequentialfind()does. No match returnsnull.assert [null, 0, '', 42, 43].findParallel() == 42
- Since:
- 6.0.1
-
findAnyParallel
Finds any element matching the predicate. May be faster thanfindParallel(java.util.Collection<T>, java.util.function.Predicate<T>)as it does not preserve encounter order. A matchingnulland no match both returnnull. -
anyParallel
Returnstrueif any element matches the predicate. -
anyParallel
Returnstrueif any element is true according to Groovy truth, as sequentialany()does.assert [0, null, 'a'].anyParallel() assert ![0, null, ''].anyParallel()
- Since:
- 6.0.1
-
everyParallel
Returnstrueif all elements match the predicate. -
everyParallel
Returnstrueif every element is true according to Groovy truth, as sequentialevery()does. An empty collection returnstrue.assert [1, 'a', [2]].everyParallel() assert ![1, 0].everyParallel()
- Since:
- 6.0.1
-
countParallel
Counts elements matching the predicate. -
minParallel
Finds the minimum element by natural ordering, as sequentialmin()does: null elements are skipped, elements compare as Groovy's<does (so numbers of different types compare by value), and the earliest of equal elements is returned. An empty collection, or one of only nulls, returnsnull.assert [4, null, 2.5, 3G].minParallel() == 2.5
- Since:
- 6.0.1
-
minParallel
Finds the minimum element using the given comparator. When several elements compare equal, the earliest in encounter order is returned. An empty collection returnsnull. A one-element collection returns that element, includingnull, without calling the comparator.Null elements are passed to the comparator, matching sequential
min(Comparator).maxParallel(java.util.Collection<T>)skips nulls instead, matching sequentialmax(Comparator). -
maxParallel
Finds the maximum element by natural ordering, as sequentialmax()does: null elements are skipped, elements compare as Groovy's>does (so numbers of different types compare by value), and the earliest of equal elements is returned. An empty collection, or one of only nulls, returnsnull.assert [4, null, 2.5, 3G].maxParallel() == 4
- Since:
- 6.0.1
-
maxParallel
Finds the maximum element using the given comparator. When several elements compare equal, the earliest in encounter order is returned. An empty collection returnsnull. A one-element collection returns that element, includingnull, without calling the comparator.Null elements are skipped, matching sequential
max(Comparator). A collection of only nulls returnsnulland does not call the comparator.minParallel(java.util.Collection<T>)passes nulls through, matching sequentialmin(Comparator). -
sumParallel
Reduces the collection in parallel using the given operator. The operator must be associative. An empty collection returnsnull. A one-element collection returns that element, includingnull, without calling the operator. Anulloperator result is returned asnull. -
sumParallel
Adds the elements together in parallel, as sequentialsum()does: each addition callspluson the left operand. An empty collection returnsnull, and a one-element collection returns that element without callingplus.The result matches
sum()only whenplusis associative for the elements. It is for integers, big decimals, strings and lists, but floating-point addition rounds, so a parallel sum offloatordoublevalues can differ fromsum()in the last digits.assert [1, 2, 3, 4].sumParallel() == 10 assert ['a', 'b', 'c'].sumParallel() == 'abc'
- Since:
- 6.0.1
-
groupByParallel
Groups elements by the classifier function in parallel. Anullkey is kept. Keys follow first-seen order, and within each group elements stay in encounter order. -
eachWithIndexParallel
Iterates over the collection in parallel with element indices. Index assignment is based on the collection's iteration order, but execution order is not guaranteed. -
collectManyParallel
public static <T,R> List<R> collectManyParallel(Collection<T> self, Function<T, ? extends Collection<R>> transform) Transforms each element into a collection and flattens the results in parallel (parallel flatMap). Outer encounter order is preserved, as is the order inside each returned collection. -
splitParallel
Partitions the collection into two lists: elements that match the predicate and elements that don't. Both lists keep encounter order.- Returns:
- a list of two lists: [matching, non-matching]
-
injectParallel
Reduces the collection in parallel with an identity value.seedis the identity ofaccumulator, and the accumulator must be associative. This matchesStream.reduce(identity, accumulator): a parallel execution may applyseedonce per chunk and then combine the chunks. A seed that is not an identity does not match a sequentialinject. An empty collection returnsseed.- Parameters:
seed- the identity value ofaccumulatoraccumulator- an associative reduction function
-
grepParallel
Filters elements using Groovy'sisCasepattern matching in parallel. Supports the same filter types as Groovy'sgrep: Class, regex Pattern, Range, Collection, Closure, etc. Encounter order is preserved. A null filter is allowed and matches the same elements as sequentialgrep(null). An element is kept whenisCaseis Groovy-true, not only when it returnsBoolean.TRUE.- Parameters:
filter- the pattern to match against (usesisCase); may be null
-
grepParallel
Returns the elements that are true according to Groovy truth, in parallel, as sequentialgrep()does. Encounter order is preserved.assert [1, 0, null, 'a', ''].grepParallel() == [1, 'a']
- Since:
- 6.0.1
-