Package net.schmizz.concurrent
Class Promise<V,T extends java.lang.Throwable>
- java.lang.Object
-
- net.schmizz.concurrent.Promise<V,T>
-
public class Promise<V,T extends java.lang.Throwable> extends java.lang.Object
Represents promised data of the parameterized typeV
and allows waiting on it. An exception may also be delivered to a waiter, and will be of the parameterized typeT
. 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 Summary
Constructors Constructor Description Promise(java.lang.String name, ExceptionChainer<T> chainer, java.util.concurrent.locks.ReentrantLock lock, LoggerFactory loggerFactory)
Creates this promise with givenname
, exceptionchainer
, and associatedlock
.Promise(java.lang.String name, ExceptionChainer<T> chainer, LoggerFactory loggerFactory)
Creates this promise with givenname
and exceptionchainer
.
-
Method Summary
Modifier and Type Method Description void
clear()
Clears this promise by setting its value and queued exception tonull
.void
deliver(V val)
Set this promise's value toval
.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 fortimeout
duration for this promise's value to be deliver.java.lang.String
toString()
V
tryRetrieve(long timeout, java.util.concurrent.TimeUnit unit)
Wait fortimeout
duration for this promise's value to be deliver.void
unlock()
Release the lock associated with this promise.
-
-
-
Constructor Detail
-
Promise
public Promise(java.lang.String name, ExceptionChainer<T> chainer, LoggerFactory loggerFactory)
Creates this promise with givenname
and exceptionchainer
. Allocates a newlock
object for this promise.- Parameters:
name
- name of this promisechainer
-ExceptionChainer
that will be used for chaining exceptions
-
Promise
public Promise(java.lang.String name, ExceptionChainer<T> chainer, java.util.concurrent.locks.ReentrantLock lock, LoggerFactory loggerFactory)
Creates this promise with givenname
, exceptionchainer
, and associatedlock
.- Parameters:
name
- name of this promisechainer
-ExceptionChainer
that will be used for chaining exceptionslock
- lock to use
-
-
Method Detail
-
deliver
public void deliver(V val)
Set this promise's value toval
. Any waiters will be delivered this value.- Parameters:
val
- the value
-
deliverError
public 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.- Parameters:
e
- the error
-
clear
public void clear()
Clears this promise by setting its value and queued exception tonull
.
-
retrieve
public V retrieve() throws T extends java.lang.Throwable
Wait indefinitely for this promise's value to be deliver.
-
retrieve
public V retrieve(long timeout, java.util.concurrent.TimeUnit unit) throws T extends java.lang.Throwable
Wait fortimeout
duration for this promise's value to be deliver.
-
tryRetrieve
public V tryRetrieve(long timeout, java.util.concurrent.TimeUnit unit) throws T extends java.lang.Throwable
Wait fortimeout
duration for this promise's value to be deliver. If the value is not deliver by the time the timeout expires, returnsnull
.
-
isDelivered
public boolean isDelivered()
- Returns:
- whether this promise has a value delivered, and no error waiting to pop.
-
inError
public boolean inError()
- Returns:
- whether this promise has been delivered an error.
-
isFulfilled
public boolean isFulfilled()
- Returns:
- whether this promise was fulfilled with either a value or an error.
-
hasWaiters
public boolean hasWaiters()
- Returns:
- whether this promise has threads waiting on it.
-
lock
public void lock()
Acquire the lock associated with this promise.
-
unlock
public void unlock()
Release the lock associated with this promise.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-