org.codehaus.gpars

groovyx.gpars
[Groovy] 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


Field Summary
private static GeneralTimer timer

Allows timeouts for async operations

 
Constructor Summary
GParsExecutorsPoolUtil()

 
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 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.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.util.Map$Entry 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.lang.Object processAnyResult(java.util.List alternatives)

Used for methods such as findAnyParallel() or anyParallel(), which may stop some alternatives once the result is known

static java.util.List processResult(java.util.List futures)

private static Pool retrieveLocalPool()

 
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()
 

Field Detail

timer

private static final GeneralTimer timer
Allows timeouts for async operations


 
Constructor Detail

GParsExecutorsPoolUtil

GParsExecutorsPoolUtil()


 
Method Detail

anyParallel

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

static boolean anyParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel any on a map


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


asyncFun

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


asyncFun

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


callAsync

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

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

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

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

static java.util.Collection collectParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel collect on a map


eachParallel

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

static java.lang.Object eachParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel each on maps


eachWithIndexParallel

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

static java.lang.Object eachWithIndexParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel eachWithIndex on maps


everyParallel

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

static boolean everyParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel every on a map


findAllParallel

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

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

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

static java.lang.Object findAnyParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel findAny on a map


findParallel

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

static java.util.Map$Entry findParallel(java.util.Map collection, groovy.lang.Closure cl)
Does parallel find on a map


grepParallel

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

static java.util.Map grepParallel(java.util.Map collection, java.lang.Object filter)
Does parallel grep on a map


groupByParallel

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

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'}* 


processAnyResult

static java.lang.Object processAnyResult(java.util.List alternatives)
Used for methods such as findAnyParallel() or anyParallel(), which may stop some alternatives once the result is known
Parameters:
alternatives - The alternative closures to run
Returns:
The result (any) found


processResult

static java.util.List processResult(java.util.List futures)


retrieveLocalPool

private static Pool retrieveLocalPool()


 

Copyright © 2008–2012 Václav Pech. All Rights Reserved.