Package groovy.concurrent
Class DataflowVariable<T>
java.lang.Object
groovy.concurrent.DataflowVariable<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
Awaitable<T>
A single-assignment variable for dataflow-style programming.
A DataflowVariable starts unbound. It can be written to exactly
once via bind(Object) (or the << operator in Groovy).
Any thread that reads the variable before it is bound will block until
a value becomes available. Once bound, all subsequent reads return the
same value immediately.
DataflowVariable implements Awaitable, so it works
naturally with await:
def x = new DataflowVariable()
def y = new DataflowVariable()
def z = Awaitable.go { await(x) + await(y) }
async { x << 10 }
async { y << 5 }
println "Result: ${await(z)}" // 15
Inspired by GPars' DataflowVariable, modernised to integrate
with Groovy's async/await and Awaitable API.
- Since:
- 6.0.0
- See Also:
-
AwaitableDataflows
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidBinds this variable to the given value.voidBinds this variable to an error.booleancancel()Attempts to cancel the computation.exceptionally(Function<Throwable, ? extends T> fn) Returns a newAwaitablethat, if this one completes exceptionally, applies the given function to the exception to produce a recovery value.get()Blocks until the computation completes and returns the result.Blocks until the computation completes or the timeout expires.booleanisBound()Returnstrueif this variable has been bound to a value or an error.booleanReturnstrueif the computation was cancelled before completing normally.booleanReturnstrueif this computation completed exceptionally (including cancellation).booleanisDone()Returnstrueif the computation has completed (normally, exceptionally, or via cancellation).Groovy operator overload:variable << valuebinds the value.<U> Awaitable<U>Returns a newAwaitablewhose result is obtained by applying the given function to this awaitable's result when it completes.<U> Awaitable<U>thenCompose(Function<? super T, ? extends Awaitable<U>> fn) Returns a newAwaitableproduced by applying the given async function to this awaitable's result, flattening the nestedAwaitable.Converts thisAwaitableto a JDKCompletableFuturefor interoperability with APIs that require it.toString()Returns the current bound state in a diagnostic form.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface groovy.concurrent.Awaitable
completeOnTimeout, completeOnTimeoutMillis, handle, orTimeout, orTimeoutMillis, thenAccept, whenComplete
-
Constructor Details
-
DataflowVariable
public DataflowVariable()Creates an unbound dataflow variable.
-
-
Method Details
-
bind
Binds this variable to the given value. Can only be called once; subsequent calls throwIllegalStateException.- Parameters:
value- the value to bind (may benull)- Throws:
IllegalStateException- if already bound
-
bindError
Binds this variable to an error. Any thread awaiting the value will receive the exception.- Parameters:
error- the error to bind- Throws:
IllegalStateException- if already bound
-
isBound
public boolean isBound()Returnstrueif this variable has been bound to a value or an error. -
leftShift
Groovy operator overload:variable << valuebinds the value.- Parameters:
value- the value to bind- Returns:
- this variable (for chaining)
-
get
Blocks until the computation completes and returns the result.- Specified by:
getin interfaceAwaitable<T>- Returns:
- the computed result
- Throws:
InterruptedException- if the calling thread is interrupted while waitingExecutionException- if the computation completed exceptionally
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Blocks until the computation completes or the timeout expires.- Specified by:
getin interfaceAwaitable<T>- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the computed result
- Throws:
InterruptedException- if the calling thread is interrupted while waitingExecutionException- if the computation completed exceptionallyTimeoutException- if the wait timed out
-
isDone
public boolean isDone()Returnstrueif the computation has completed (normally, exceptionally, or via cancellation). -
cancel
public boolean cancel()Attempts to cancel the computation. If the computation has not yet started or is still running, it will be cancelled with aCancellationException. -
isCancelled
public boolean isCancelled()Returnstrueif the computation was cancelled before completing normally.- Specified by:
isCancelledin interfaceAwaitable<T>- Returns:
trueif cancelled
-
isCompletedExceptionally
public boolean isCompletedExceptionally()Returnstrueif this computation completed exceptionally (including cancellation).- Specified by:
isCompletedExceptionallyin interfaceAwaitable<T>- Returns:
trueif completed with an error or cancellation
-
then
Returns a newAwaitablewhose result is obtained by applying the given function to this awaitable's result when it completes. -
thenCompose
Returns a newAwaitableproduced by applying the given async function to this awaitable's result, flattening the nestedAwaitable. This is the monadicflatMapoperation for awaitables.- Specified by:
thenComposein interfaceAwaitable<T>- Type Parameters:
U- the type of the inner awaitable's result- Parameters:
fn- the async mapping function that returns anAwaitable- Returns:
- a new awaitable holding the inner result
-
exceptionally
Returns a newAwaitablethat, if this one completes exceptionally, applies the given function to the exception to produce a recovery value. The throwable passed to the function is deeply unwrapped to strip JDK wrapper layers.- Specified by:
exceptionallyin interfaceAwaitable<T>- Parameters:
fn- the recovery function- Returns:
- a new awaitable that recovers from failures
-
toCompletableFuture
Converts thisAwaitableto a JDKCompletableFuturefor interoperability with APIs that require it.- Specified by:
toCompletableFuturein interfaceAwaitable<T>- Returns:
- a
CompletableFuturerepresenting this computation
-
toString
Returns the current bound state in a diagnostic form.
-