public class Promise<V,T extends java.lang.Throwable>
extends java.lang.Object
V
and allows waiting on it. An exception may also be
delivered to a waiter, and will be of the parameterized type T
.
For atomic operations on a promise, e.g. checking if a value is delivered and if it is not then setting it, the
associated lock for the promise should be acquired while doing so.Constructor and Description |
---|
Promise(java.lang.String name,
ExceptionChainer<T> chainer)
Creates this promise with given
name and exception chainer . |
Promise(java.lang.String name,
ExceptionChainer<T> chainer,
java.util.concurrent.locks.ReentrantLock lock)
Creates this promise with given
name , exception chainer , and associated lock . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this promise by setting its value and queued exception to
null . |
void |
deliver(V val)
Set this promise's value to
val . |
void |
deliverError(java.lang.Throwable e)
Queues error that will be thrown in any waiting thread or any thread that attempts to wait on this promise
hereafter.
|
boolean |
hasWaiters() |
boolean |
inError() |
boolean |
isDelivered() |
boolean |
isFulfilled() |
void |
lock()
Acquire the lock associated with this promise.
|
V |
retrieve()
Wait indefinitely for this promise's value to be deliver.
|
V |
retrieve(long timeout,
java.util.concurrent.TimeUnit unit)
Wait for
timeout duration for this promise's value to be deliver. |
java.lang.String |
toString() |
V |
tryRetrieve(long timeout,
java.util.concurrent.TimeUnit unit)
Wait for
timeout duration for this promise's value to be deliver. |
void |
unlock()
Release the lock associated with this promise.
|
public Promise(java.lang.String name, ExceptionChainer<T> chainer)
name
and exception chainer
. Allocates a new lock
object for this promise.name
- name of this promisechainer
- ExceptionChainer
that will be used for chaining exceptionspublic Promise(java.lang.String name, ExceptionChainer<T> chainer, java.util.concurrent.locks.ReentrantLock lock)
name
, exception chainer
, and associated lock
.name
- name of this promisechainer
- ExceptionChainer
that will be used for chaining exceptionslock
- lock to usepublic void deliver(V val)
val
. Any waiters will be delivered this value.val
- the valuepublic void deliverError(java.lang.Throwable e)
e
- the errorpublic void clear()
null
.public V retrieve() throws T extends java.lang.Throwable
T
- in case another thread informs the promise of an error meanwhileT extends java.lang.Throwable
public V retrieve(long timeout, java.util.concurrent.TimeUnit unit) throws T extends java.lang.Throwable
timeout
duration for this promise's value to be deliver.timeout
- the timeoutunit
- time unit for the timeoutT
- in case another thread informs the promise of an error meanwhile, or the timeout expiresT extends java.lang.Throwable
public V tryRetrieve(long timeout, java.util.concurrent.TimeUnit unit) throws T extends java.lang.Throwable
timeout
duration for this promise's value to be deliver.
If the value is not deliver by the time the timeout expires, returns null
.timeout
- the timeoutunit
- time unit for the timeoutnull
T
- in case another thread informs the promise of an error meanwhileT extends java.lang.Throwable
public boolean isDelivered()
public boolean inError()
public boolean isFulfilled()
public boolean hasWaiters()
public void lock()
public void unlock()
public java.lang.String toString()
toString
in class java.lang.Object