Enables a ExecutorService-based DSL on closures, objects and collections.
E.g.
GParsExecutorsPool.withPool(5) {ExecutorService service ->
Collection
Modifiers | Name | Description |
---|---|---|
private static ThreadLocalPools |
currentPoolStack |
Maps threads to their appropriate thread pools |
private static int |
defaultPoolSize |
Caches the default pool size. |
Constructor and description |
---|
GParsExecutorsPool
() |
Type | Name and description |
---|---|
private static java.util.concurrent.ThreadFactory |
createDefaultThreadFactory() |
private static java.lang.Object |
createPool() Creates a new pool with the default size() |
private static java.lang.Object |
createPool(int poolSize) Creates a new pool with the given size() |
private static java.lang.Object |
createPool(int poolSize, java.util.concurrent.ThreadFactory threadFactory) |
static java.util.List<java.util.concurrent.Future<java.lang.Object>> |
executeAsync(groovy.lang.Closure... closures) Starts multiple closures in separate threads, collecting Futures for their return values Reuses the pool defined by the surrounding withPool() call. |
static java.util.List<java.util.concurrent.Future<java.lang.Object>> |
executeAsync(java.util.List<groovy.lang.Closure> closures) Starts multiple closures in separate threads, collecting Futures for their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method. |
static java.util.List<java.lang.Object> |
executeAsyncAndWait(groovy.lang.Closure... closures) Starts multiple closures in separate threads, collecting their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method. |
static java.util.List<java.lang.Object> |
executeAsyncAndWait(java.util.List<groovy.lang.Closure> closures) Starts multiple closures in separate threads, collecting their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method. |
protected static java.util.concurrent.ExecutorService |
retrieveCurrentPool() Retrieves the pool assigned to the current thread. |
static void |
shutdown() |
static java.lang.Object |
speculate(java.util.List<groovy.lang.Closure> alternatives) Runs the supplied closures asynchronously and in parallel, returning the first result obtained and cancelling the other (slower) calculations. |
static java.lang.Object |
speculate(groovy.lang.Closure... alternatives) Runs the supplied closures asynchronously and in parallel, returning the first result obtained and cancelling the other (slower) calculations. |
static java.lang.Object |
withExistingPool(java.util.concurrent.ExecutorService pool, groovy.lang.Closure cl) Creates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. |
static java.lang.Object |
withPool(groovy.lang.Closure cl) Creates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. |
static java.lang.Object |
withPool(int numberOfThreads, groovy.lang.Closure cl) Creates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. |
static java.lang.Object |
withPool(int numberOfThreads, java.util.concurrent.ThreadFactory threadFactory, groovy.lang.Closure cl) Creates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. |
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() |
Maps threads to their appropriate thread pools
Caches the default pool size.
Creates a new pool with the default size()
Creates a new pool with the given size()
Starts multiple closures in separate threads, collecting Futures for their return values Reuses the pool defined by the surrounding withPool() call. If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method.
Starts multiple closures in separate threads, collecting Futures for their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method.
Starts multiple closures in separate threads, collecting their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method.
Starts multiple closures in separate threads, collecting their return values If an exception is thrown from the closure when called on any of the collection's elements, it will be re-thrown in the calling thread when it calls the Future.get() method.
Retrieves the pool assigned to the current thread.
Runs the supplied closures asynchronously and in parallel, returning the first result obtained and cancelling the other (slower) calculations. Typically used to run several different calculations in parallel, all of which are supposed to give the same result, but may last different amount of time each. If the system has enough threads available, the calculations can be test-run in parallel and the fastest result is then used, while the other results are cancelled or discarded.
alternatives
- All the functions to invoke in parallelRuns the supplied closures asynchronously and in parallel, returning the first result obtained and cancelling the other (slower) calculations. Typically used to run several different calculations in parallel, all of which are supposed to give the same result, but may last different amount of time each. If the system has enough threads available, the calculations can be test-run in parallel and the fastest result is then used, while the other results are cancelled or discarded.
alternatives
- All the functions to invoke in parallelCreates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. Within the supplied code block the ExecutorService is available as the only parameter, objects have been enhanced with the eachParallel(), collectParallel() and other methods from the GParsExecutorsPoolUtil category class as well as closures can be turned into asynchronous ones by calling the async() method on them. E.g. closure,async returns a new closure, which, when run will schedule the original closure for processing in the pool. Calling images.eachParallel{processImage(it}} will call the potentially long-lasting processImage() operation on each image in the images collection in parallel.
def result = new ConcurrentSkipListSet() GParsExecutorsPool.withPool(5) {ExecutorService service -> [1, 2, 3, 4, 5].eachParallel{Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
pool
- The ExecutorService to use, the service will not be shutdown after this method returnsCreates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. It is an identical alternative for withPool() with a shorter name. Within the supplied code block the ExecutorService is available as the only parameter, objects have been enhanced with the eachParallel(), collectParallel() and other methods from the GParsExecutorsPoolUtil category class as well as closures can be turned into asynchronous ones by calling the async() method on them. E.g. closure,async returns a new closure, which, when run will schedule the original closure for processing in the pool. Calling images.eachParallel{processImage(it}} will call the potentially long-lasting processImage() operation on each image in the images collection in parallel.
def result = new ConcurrentSkipListSet() GParsExecutorsPool.withPool {ExecutorService service -> [1, 2, 3, 4, 5].eachParallel{Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
cl
- The block of code to invoke with the DSL enabledCreates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. It is an identical alternative for withPool() with a shorter name. Within the supplied code block the ExecutorService is available as the only parameter, objects have been enhanced with the eachParallel(), collectParallel() and other methods from the GParsExecutorsPoolUtil category class as well as closures can be turned into asynchronous ones by calling the async() method on them. E.g. closure,async returns a new closure, which, when run will schedule the original closure for processing in the pool. Calling images.eachParallel{processImage(it}} will call the potentially long-lasting processImage() operation on each image in the images collection in parallel.
def result = new ConcurrentSkipListSet() GParsExecutorsPool.withPool(5) {ExecutorService service -> [1, 2, 3, 4, 5].eachParallel{Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
numberOfThreads
- Number of threads in the newly created thread poolcl
- The block of code to invoke with the DSL enabledCreates a new instance of ExecutorService, binds it to the current thread, enables the ExecutorService DSL and runs the supplied closure. It is an identical alternative for withPool() with a shorter name. Within the supplied code block the ExecutorService is available as the only parameter, objects have been enhanced with the eachParallel(), collectParallel() and other methods from the GParsExecutorsPoolUtil category class as well as closures can be turned into asynchronous ones by calling the async() method on them. E.g. closure,async returns a new closure, which, when run will schedule the original closure for processing in the pool. Calling images.eachParallel{processImage(it}} will call the potentially long-lasting processImage() operation on each image in the images collection in parallel.
def result = new ConcurrentSkipListSet() GParsExecutorsPool.withPool(5) {ExecutorService service -> [1, 2, 3, 4, 5].eachParallel{Number number -> result.add(number * 10)}* assertEquals(new HashSet([10, 20, 30, 40, 50]), result) }*
numberOfThreads
- Number of threads in the newly created thread poolthreadFactory
- Factory for threads in the poolcl
- The block of code to invoke with the DSL enabledCopyright © 2008–2014 Václav Pech. All Rights Reserved.