Groovy Documentation

gpars
Class GParsExecutorsPool

java.lang.Object
  gpars.GParsExecutorsPool

class GParsExecutorsPool

Enables a ExecutorService-based DSL on closures, objects and collections. E.g. GParsExecutorsPool.withPool(5) {ExecutorService service -> Collection result = [1, 2, 3, 4, 5].collectParallel({it * 10}.async()) assertEquals(new HashSet([10, 20, 30, 40, 50]), new HashSet((Collection)result*.get())) }* GParsExecutorsPool.withPool(5) {ExecutorService service -> def result = [1, 2, 3, 4, 5].findParallel{Number number -> number > 2}* assert result in [3, 4, 5] }*

author:
Vaclav Pech Date: Oct 23, 2008


Constructor Summary
GParsExecutorsPool()

 
Method Summary
static List executeAsync(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 List executeAsync(List 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 List 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 List executeAsyncAndWait(List 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 ExecutorService retrieveCurrentPool()

Retrieves the pool assigned to the current thread.

static def withExistingPool(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 def 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 def 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 def withPool(int numberOfThreads, 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 Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Constructor Detail

GParsExecutorsPool

GParsExecutorsPool()


 
Method Detail

executeAsync

public static List executeAsync(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.
return:
Futures for the result values or exceptions of all closures


executeAsync

public static List executeAsync(List 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.
return:
Futures for the result values or exceptions of all closures


executeAsyncAndWait

public static List 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.
return:
The result values of all closures
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.


executeAsyncAndWait

public static List executeAsyncAndWait(List 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.
return:
The result values of all closures
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.


retrieveCurrentPool

protected static ExecutorService retrieveCurrentPool()
Retrieves the pool assigned to the current thread.


withExistingPool

public static def withExistingPool(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. 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)
}* 
param:
pool The ExecutorService to use, the service will not be shutdown after this method returns


withPool

public static def 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. 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)
}* 
param:
cl The block of code to invoke with the DSL enabled


withPool

public static def 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. 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)
}* 
param:
numberOfThreads Number of threads in the newly created thread pool
param:
cl The block of code to invoke with the DSL enabled


withPool

public static def withPool(int numberOfThreads, 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. 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)
}* 
param:
numberOfThreads Number of threads in the newly created thread pool
param:
threadFactory Factory for threads in the pool
param:
cl The block of code to invoke with the DSL enabled


 

Groovy Documentation