public class DataflowVariable<T>
extends Object
implements Awaitable
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.
T - the value type| Constructor and description |
|---|
DataflowVariable()Creates an unbound dataflow variable. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
bind(T value)Binds this variable to the given value. |
|
public void |
bindError(Throwable error)Binds this variable to an error. |
|
public boolean |
cancel()* Attempts to cancel the computation. If the computation has not yet started * or is still running, it will be cancelled with a CancellationException. * *
|
|
public Awaitable<T> |
exceptionally(Function<Throwable, ? extends T> fn)* Returns a new Awaitable that, 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.
*
*
|
|
public T |
get()* Blocks until the computation completes and returns the result. * *
|
|
public T |
get(long timeout, TimeUnit unit)* Blocks until the computation completes or the timeout expires. * *
|
|
public boolean |
isBound()Returns true if this variable has been bound to a value
or an error. |
|
public boolean |
isCancelled()* Returns true if the computation was cancelled before completing normally.
*
*
|
|
public boolean |
isCompletedExceptionally()* Returns true if this computation completed exceptionally
* (including cancellation).
*
*
|
|
public boolean |
isDone()* Returns true if the computation has completed (normally,
* exceptionally, or via cancellation).
*
*
|
|
public DataflowVariable<T> |
leftShift(T value)Groovy operator overload: variable << value binds the value. |
<U> |
public Awaitable<U> |
then(Function<? super T, ? extends U> fn)* Returns a new Awaitable whose result is obtained by applying the
* given function to this awaitable's result when it completes.
*
*
|
<U> |
public Awaitable<U> |
thenCompose(Function<? super T, ? extends Awaitable<U>> fn)* Returns a new Awaitable produced by applying the given async
* function to this awaitable's result, flattening the nested Awaitable.
* This is the monadic flatMap operation for awaitables.
*
*
|
|
public CompletableFuture<T> |
toCompletableFuture()* Converts this Awaitable to a JDK CompletableFuture
* for interoperability with APIs that require it.
*
*
|
|
public String |
toString()Returns the current bound state in a diagnostic form. |
Binds this variable to the given value. Can only be called once; subsequent calls throw IllegalStateException.
value - the value to bind (may be null)Binds this variable to an error. Any thread awaiting the value will receive the exception.
error - the error to bind* Attempts to cancel the computation. If the computation has not yet started * or is still running, it will be cancelled with a CancellationException. * *
true if the computation was successfully cancelled
* Returns a new Awaitable that, 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.
*
*
fn - the recovery function
** Blocks until the computation completes and returns the result. * *
* Blocks until the computation completes or the timeout expires. * *
timeout - the maximum time to wait
*unit - the time unit of the timeout argument
* Returns true if this variable has been bound to a value
or an error.
* Returns true if the computation was cancelled before completing normally.
*
*
true if cancelled
* Returns true if this computation completed exceptionally
* (including cancellation).
*
*
true if completed with an error or cancellation
* Returns true if the computation has completed (normally,
* exceptionally, or via cancellation).
*
*
true if complete Groovy operator overload: variable << value binds the value.
value - the value to bind
* Returns a new Awaitable whose result is obtained by applying the
* given function to this awaitable's result when it completes.
*
*
fn - the mapping function
*U - the type of the mapped result
*
* Returns a new Awaitable produced by applying the given async
* function to this awaitable's result, flattening the nested Awaitable.
* This is the monadic flatMap operation for awaitables.
*
*
fn - the async mapping function that returns an Awaitable
*U - the type of the inner awaitable's result
*
* Converts this Awaitable to a JDK CompletableFuture
* for interoperability with APIs that require it.
*
*
CompletableFuture representing this computationReturns the current bound state in a diagnostic form.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.