This class forms the core of the DSL initialized by GParsExecutorsPool. The static methods of GParsExecutorsPoolUtil get attached to their first arguments (the Groovy Category mechanism) and can be then invoked as if they were part of the argument classes.
Modifiers | Name | Description |
---|---|---|
private static GeneralTimer |
timer |
Allows timeouts for async operations |
Constructor and description |
---|
GParsExecutorsPoolUtil
() |
Type | Name and description |
---|---|
static boolean |
anyParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the any() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static boolean |
anyParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel any on a map |
static groovy.lang.Closure |
async(groovy.lang.Closure cl) Creates an asynchronous variant of the supplied closure, which, when invoked returns a future for the potential return value |
static groovy.lang.Closure |
asyncFun(groovy.lang.Closure original, boolean blocking = false) Creates an asynchronous and composable variant of the supplied closure, which, when invoked returns a DataflowVariable for the potential return value |
static groovy.lang.Closure |
asyncFun(groovy.lang.Closure original, Pool pool, boolean blocking = false) Creates an asynchronous and composable variant of the supplied closure, which, when invoked returns a DataflowVariable for the potential return value |
static java.util.concurrent.Future |
callAsync(groovy.lang.Closure cl, java.lang.Object... args) Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value. |
private static java.util.concurrent.Future |
callParallel(groovy.lang.Closure task) schedules the supplied closure for processing in the underlying thread pool. |
static java.util.concurrent.Future |
callTimeoutAsync(groovy.lang.Closure cl, long timeout, java.lang.Object... args) Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value. |
static java.util.concurrent.Future |
callTimeoutAsync(groovy.lang.Closure cl, groovy.time.Duration timeout, java.lang.Object... args) Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value. |
static java.util.Collection<java.lang.Object> |
collectParallel(java.lang.Object collection, groovy.lang.Closure cl) Iterates over a collection/object with the collect() method using an asynchronous variant of the supplied closure to evaluate each collection's element. |
static java.util.Collection<java.lang.Object> |
collectParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel collect on a map |
static java.lang.Object |
eachParallel(java.lang.Object collection, groovy.lang.Closure cl) Iterates over a collection/object with the each() method using an asynchronous variant of the supplied closure to evaluate each collection's element. |
static java.lang.Object |
eachParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel each on maps |
static java.lang.Object |
eachWithIndexParallel(java.lang.Object collection, groovy.lang.Closure cl) Iterates over a collection/object with the eachWithIndex() method using an asynchronous variant of the supplied closure to evaluate each collection's element. |
static java.lang.Object |
eachWithIndexParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel eachWithIndex on maps |
static boolean |
everyParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the all() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static boolean |
everyParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel every on a map |
static java.util.Collection<java.lang.Object> |
findAllParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the findAll() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static java.util.Map<K, V> |
findAllParallel(java.util.Map<K, V> collection, groovy.lang.Closure cl) Does parallel findAll on a map returning a map of found items |
static java.lang.Object |
findAnyParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the find() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static java.lang.Object |
findAnyParallel(java.util.Map collection, groovy.lang.Closure cl) Does parallel findAny on a map |
static java.lang.Object |
findParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the find() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static Map.Entry<K, V> |
findParallel(java.util.Map<K, V> collection, groovy.lang.Closure cl) Does parallel find on a map |
static java.lang.Object |
grepParallel(java.lang.Object collection, java.lang.Object filter) Performs the grep()() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static java.util.Map<K, V> |
grepParallel(java.util.Map<K, V> collection, java.lang.Object filter) Does parallel grep on a map |
static java.util.Map |
groupByParallel(java.lang.Object collection, groovy.lang.Closure cl) Performs the groupBy() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. |
static java.util.concurrent.Future |
leftShift(java.util.concurrent.ExecutorService executorService, groovy.lang.Closure task) Submits the task for asynchronous processing returning the Future received from the executor service. |
static java.lang.Object |
processAnyResult(java.util.List<groovy.lang.Closure> alternatives) Used for methods such as findAnyParallel() or anyParallel(), which may stop some alternatives once the result is known |
static java.util.List<java.lang.Object> |
processResult(java.util.List<java.util.concurrent.Future<java.lang.Object>> futures) |
private static Pool |
retrieveLocalPool() |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(), java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Allows timeouts for async operations
Performs the any() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. The anyParallel() method is lazy and once a positive answer has been given by at least one element, it avoids running the supplied closure on subsequent elements. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert service.anyParallel([1, 2, 3, 4, 5]){Number number -> number > 2}* assert !service.anyParallel([1, 2, 3, 4, 5]){Number number -> number > 6}*}* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new anyParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert [1, 2, 3, 4, 5].anyParallel{Number number -> number > 2}* assert ![1, 2, 3, 4, 5].anyParallel{Number number -> number > 6}*}*
Does parallel any on a map
Creates an asynchronous variant of the supplied closure, which, when invoked returns a future for the potential return value
Creates an asynchronous and composable variant of the supplied closure, which, when invoked returns a DataflowVariable for the potential return value
Creates an asynchronous and composable variant of the supplied closure, which, when invoked returns a DataflowVariable for the potential return value
Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value.
schedules the supplied closure for processing in the underlying thread pool.
Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value. Also allows the asynchronous calculation to be cancelled after a given timeout. In order to allow cancellation, the asynchronously running code must keep checking the _interrupted_ flag of its own thread and cease the calculation once the flag is set to true.
timeout
- The timeout in milliseconds to wait before the calculation gets cancelled.Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value. Also allows the asynchronous calculation to be cancelled after a given timeout. In order to allow cancellation, the asynchronously running code must keep checking the _interrupted_ flag of its own thread and cease the calculation once the flag is set to true.
timeout
- The timeout to wait before the calculation gets cancelled.Iterates over a collection/object with the collect() method using an asynchronous variant of the supplied closure to evaluate each collection's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = service.collectParallel([1, 2, 3, 4, 5]){Number number -> number * 10}* assertEquals(new HashSet([10, 20, 30, 40, 50]), new HashSet((Collection)result)) }* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new collectParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].collectParallel{Number number -> number * 10}* assertEquals(new HashSet([10, 20, 30, 40, 50]), new HashSet((Collection)result)) }*
Does parallel collect on a map
Iterates over a collection/object with the each() method using an asynchronous variant of the supplied closure to evaluate each collection's element. A Semaphore is used to make the calling thread wait for all the results. After this method returns, all the closures have been finished and all the potential shared resources have been updated by the threads. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. Example: GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = Collections.synchronizedSet(new HashSet()) service.eachParallel([1, 2, 3, 4, 5]) {Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }* Note that the result variable is synchronized to prevent race conditions between multiple threads. Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new eachParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = Collections.synchronizedSet(new HashSet()) [1, 2, 3, 4, 5].eachParallel { Number number -> result.add(number * 10) }* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
Does parallel each on maps
Iterates over a collection/object with the eachWithIndex() method using an asynchronous variant of the supplied closure to evaluate each collection's element. A Semaphore is used to make the calling thread wait for all the results. After this method returns, all the closures have been finished and all the potential shared resources have been updated by the threads. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. Example: GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = Collections.synchronizedSet(new HashSet()) service.eachWithIndexParallel([1, 2, 3, 4, 5]) {Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }* Note that the result variable is synchronized to prevent race conditions between multiple threads. Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new eachParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = Collections.synchronizedSet(new HashSet()) [1, 2, 3, 4, 5].eachWithIndexParallel { Number number, int index -> result.add(number * 10) }* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
Does parallel eachWithIndex on maps
Performs the all() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert service.everyParallel([1, 2, 3, 4, 5]){Number number -> number > 0}* assert !service.everyParallel([1, 2, 3, 4, 5]){Number number -> number > 2}*}* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new findAllParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert [1, 2, 3, 4, 5].everyParallel{Number number -> number > 0}* assert ![1, 2, 3, 4, 5].everyParallel{Number number -> number > 2}*}*
Does parallel every on a map
Performs the findAll() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = service.findAllParallel([1, 2, 3, 4, 5]){Number number -> number > 2}* assertEquals(new HashSet([3, 4, 5]), new HashSet((Collection)result)) }* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new findAllParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].findAllParallel{Number number -> number > 2}* assertEquals(new HashSet([3, 4, 5]), new HashSet((Collection)result)) }*
Does parallel findAll on a map returning a map of found items
Performs the find() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. Unlike with the find method, findAnyParallel() does not guarantee that the a matching element with the lowest index is returned. The findAnyParallel() method evaluates elements lazily and stops processing further elements of the collection once a match has been found. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = service.findParallel([1, 2, 3, 4, 5]){Number number -> number > 2}* assert result in [3, 4, 5] }* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new findAllParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].findParallel{Number number -> number > 2}* assert result in [3, 4, 5] }*
Does parallel findAny on a map
Performs the find() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = service.findParallel([1, 2, 3, 4, 5]){Number number -> number > 2}* assert result in [3, 4, 5] }* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new findAllParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].findParallel{Number number -> number > 2}* assert result in [3, 4, 5] }*
Does parallel find on a map
Performs the grep()() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = service.grepParallel([1, 2, 3, 4, 5])(3..6) assertEquals(new HashSet([3, 4, 5]), new HashSet((Collection)result)) }* Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new findAllParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].grepParallel(3..6) assertEquals(new HashSet([3, 4, 5]), new HashSet((Collection)result)) }*
Does parallel grep on a map
Performs the groupBy() operation using an asynchronous variant of the supplied closure to evaluate each collection's/object's element. After this method returns, all the closures have been finished and the caller can safely use the result. It's important to protect any shared resources used by the supplied closure from race conditions caused by multi-threaded access. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert service.groupByParallel(([1, 2, 3, 4, 5]){Number number -> number % 2}).size() == 2 Alternatively a DSL can be used to simplify the code. All collections/objects within the withPool block have a new groupByParallel(Closure cl) method, which delegates to the GParsExecutorsPoolUtil class. GParsExecutorsPool.withPool(5) {ExecutorService service -> assert ([1, 2, 3, 4, 5].groupByParallel{Number number -> number % 2}).size() == 2
Submits the task for asynchronous processing returning the Future received from the executor service. Allows for the following syntax:
executorService << {println 'Inside parallel task'}*
Used for methods such as findAnyParallel() or anyParallel(), which may stop some alternatives once the result is known
alternatives
- The alternative closures to runCopyright © 2008–2014 Václav Pech. All Rights Reserved.