gpars
[Groovy] Class GParsExecutorsPoolUtil
java.lang.Object
gpars.GParsExecutorsPoolUtil
class GParsExecutorsPoolUtil
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.
- Authors:
- Vaclav Pech
Date: Oct 23, 2008
- See Also:
- groovyx.gpars.GParsExecutorsPool
Method Summary |
static boolean
|
anyParallel(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 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 Future
|
callAsync(groovy.lang.Closure cl, Object... args)
Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value,
|
static def
|
collectParallel(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 def
|
eachParallel(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 def
|
eachWithIndexParallel(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 boolean
|
everyParallel(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 def
|
findAllParallel(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 def
|
findAnyParallel(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 def
|
findParallel(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 def
|
grepParallel(Object collection, def filter)
Performs the grep()() operation using an asynchronous variant of the supplied closure
to evaluate each collection's/object's element.
|
static Map
|
groupByParallel(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 Future
|
leftShift(ExecutorService executorService, groovy.lang.Closure task)
Submits the task for asynchronous processing returning the Future received from the executor service.
|
static List
|
processResult(List futures)
|
anyParallel
static boolean anyParallel(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.
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}*}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
async
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
callAsync
static Future callAsync(groovy.lang.Closure cl, Object... args)
- Calls a closure in a separate thread supplying the given arguments, returning a future for the potential return value,
collectParallel
static def collectParallel(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.
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))
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
eachParallel
static def eachParallel(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. 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)
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
eachWithIndexParallel
static def eachWithIndexParallel(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. 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)
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
everyParallel
static boolean everyParallel(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.
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}*}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
findAllParallel
static def findAllParallel(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.
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))
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
findAnyParallel
static def findAnyParallel(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. 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]
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
findParallel
static def findParallel(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.
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]
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
grepParallel
static def grepParallel(Object collection, def filter)
- 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))
}*
- throws:
- AsyncException If any of the collection's elements causes the closure to throw an exception. The original exceptions will be stored in the AsyncException's concurrentExceptions field.
groupByParallel
static Map groupByParallel(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.
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
leftShift
static Future leftShift(ExecutorService executorService, groovy.lang.Closure task)
- Submits the task for asynchronous processing returning the Future received from the executor service.
Allows for the following syntax:
executorService << {println 'Inside parallel task'}*
processResult
static List processResult(List futures)
-
Groovy Documentation