groovyx.gpars
Class GParsExecutorsPoolUtil
java.lang.Object
groovyx.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:
- GParsExecutorsPool
Property Summary |
private static java.util.Timer |
timer
Allows timeouts for async operations
|
Method Summary |
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 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.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
|
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.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
|
findAllParallel(java.util.Map 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 java.lang.Object
|
findParallel(java.util.Map 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
|
grepParallel(java.util.Map 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.util.List
|
processResult(java.util.List futures)
|
Methods inherited from class java.lang.Object
|
java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#wait(long, int), 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() |
timer
private static final java.util.Timer timer
- Allows timeouts for async operations
GParsExecutorsPoolUtil
GParsExecutorsPoolUtil()
-
anyParallel
public 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.
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.
anyParallel
public static boolean anyParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel any on a map
async
public 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
public 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.
callParallel
private static java.util.concurrent.Future callParallel(groovy.lang.Closure task)
- schedules the supplied closure for processing in the underlying thread pool.
callTimeoutAsync
public 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.
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.
- Parameters:
timeout
- The timeout in milliseconds to wait before the calculation gets cancelled.
callTimeoutAsync
public 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.
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.
- Parameters:
timeout
- The timeout to wait before the calculation gets cancelled.
collectParallel
public static 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.
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.
collectParallel
public static java.util.Collection collectParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel collect on a map
eachParallel
public 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. 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.
eachParallel
public static java.lang.Object eachParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel each on maps
eachWithIndexParallel
public 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. 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.
eachWithIndexParallel
public static java.lang.Object eachWithIndexParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel eachWithIndex on maps
everyParallel
public 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.
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.
everyParallel
public static boolean everyParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel every on a map
findAllParallel
public static 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.
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.
findAllParallel
public static java.util.Map findAllParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel findAll on a map returning a map of found items
findAnyParallel
public 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. 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.
findAnyParallel
public static java.lang.Object findAnyParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel findAny on a map
findParallel
public 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.
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
public static java.lang.Object findParallel(java.util.Map collection, groovy.lang.Closure cl)
- Does parallel find on a map
grepParallel
public 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.
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.
grepParallel
public static java.util.Map grepParallel(java.util.Map collection, java.lang.Object filter)
- Does parallel grep on a map
groupByParallel
public 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.
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
public 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.
Allows for the following syntax:
executorService << {println 'Inside parallel task'}*
processResult
public static java.util.List processResult(java.util.List futures)
-
Copyright © 2008–2010 Václav Pech. All Rights Reserved.