Class Val<V>
- java.lang.Object
-
- co.paralleluniverse.strands.dataflow.Val<V>
-
- All Implemented Interfaces:
java.util.concurrent.Future<V>
public class Val<V> extends java.lang.Object implements java.util.concurrent.Future<V>
A dataflow constant. Represents a delayed value that can be set at most once, and when read, blocks until a value has been set.
-
-
Constructor Summary
Constructors Constructor Description Val()
Val(FiberScheduler scheduler, SuspendableCallable<V> f)
Creates aVal
whose value will be the one returned by the givenSuspendableCallable
, which will be spawned into a new fiber, scheduled by the givenFiberScheduler
.Val(SuspendableCallable<V> f)
Creates aVal
whose value will be the one returned by the givenSuspendableCallable
, which will be spawned into a new fiber.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
cancel(boolean mayInterruptIfRunning)
ThrowsUnsupportedOperationException
.V
get()
Returns the delayed value, blocking until it has been set.V
get(long timeout, java.util.concurrent.TimeUnit unit)
Returns the delayed value, blocking until it has been set, but no longer than the given timeout.V
get(Timeout timeout)
protected SimpleConditionSynchronizer
getSync()
boolean
isCancelled()
boolean
isDone()
void
set(V value)
Sets the value.void
setException(java.lang.Throwable t)
Sets an exception that will be thrown byget
, wrapped byRuntimeExecutionException
.
-
-
-
Constructor Detail
-
Val
public Val(SuspendableCallable<V> f)
Creates aVal
whose value will be the one returned by the givenSuspendableCallable
, which will be spawned into a new fiber.- Parameters:
f
- The function that will compute thisVal
's value in a newly spawned fiber
-
Val
public Val(FiberScheduler scheduler, SuspendableCallable<V> f)
Creates aVal
whose value will be the one returned by the givenSuspendableCallable
, which will be spawned into a new fiber, scheduled by the givenFiberScheduler
.- Parameters:
scheduler
- the scheduler in which the new fiber will be spawned.f
- The function that will compute thisVal
's value in a newly spawned fiber
-
Val
public Val()
-
-
Method Detail
-
set
public final void set(V value)
Sets the value. If the value has already been set (or if a function has been supplied to the constructor), this method will throw anIllegalStateException
. However, you should not rely on this behavior, as the implementation is free to silently ignore additional attempts to set the value.- Parameters:
value
- the value- Throws:
java.lang.IllegalStateException
- if the value has already been set.
-
setException
public final void setException(java.lang.Throwable t)
Sets an exception that will be thrown byget
, wrapped byRuntimeExecutionException
.- Parameters:
t
- the exception- Throws:
java.lang.IllegalStateException
- if the value has already been set.
-
isDone
public boolean isDone()
- Specified by:
isDone
in interfacejava.util.concurrent.Future<V>
-
getSync
protected SimpleConditionSynchronizer getSync()
-
get
@Suspendable public V get() throws java.lang.InterruptedException
Returns the delayed value, blocking until it has been set.- Specified by:
get
in interfacejava.util.concurrent.Future<V>
- Returns:
- the value
- Throws:
java.lang.InterruptedException
-
get
@Suspendable public V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
Returns the delayed value, blocking until it has been set, but no longer than the given timeout.- Specified by:
get
in interfacejava.util.concurrent.Future<V>
- Parameters:
timeout
- The maximum duration to block waiting for the value to be set.unit
- The time unit of the timeout value.- Returns:
- the value
- Throws:
java.util.concurrent.TimeoutException
- if the timeout expires before the value is set.java.lang.InterruptedException
-
get
@Suspendable public V get(Timeout timeout) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
- Throws:
java.lang.InterruptedException
java.util.concurrent.TimeoutException
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
ThrowsUnsupportedOperationException
.- Specified by:
cancel
in interfacejava.util.concurrent.Future<V>
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<V>
-
-