public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V>
Modifier and Type | Class and Description |
---|---|
private static class |
DefaultPromise.CauseHolder |
Modifier and Type | Field and Description |
---|---|
private static DefaultPromise.CauseHolder |
CANCELLATION_CAUSE_HOLDER |
private EventExecutor |
executor |
private java.lang.Object |
listeners
One or more listeners.
|
private static InternalLogger |
logger |
private static int |
MAX_LISTENER_STACK_DEPTH |
private boolean |
notifyingListeners
Threading - synchronized(this).
|
private static InternalLogger |
rejectedExecutionLogger |
private java.lang.Object |
result |
private static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DefaultPromise,java.lang.Object> |
RESULT_UPDATER |
private static Signal |
SUCCESS |
private static Signal |
UNCANCELLABLE |
private short |
waiters
Threading - synchronized(this).
|
Modifier | Constructor and Description |
---|---|
protected |
DefaultPromise()
See
executor() for expectations of the executor. |
|
DefaultPromise(EventExecutor executor)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
Promise<V> |
addListener(GenericFutureListener<? extends Future<? super V>> listener)
Adds the specified listener to this future.
|
private void |
addListener0(GenericFutureListener<? extends Future<? super V>> listener) |
Promise<V> |
addListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Adds the specified listeners to this future.
|
Promise<V> |
await()
Waits for this future to be completed.
|
boolean |
await(long timeoutMillis)
Waits for this future to be completed within the
specified time limit.
|
boolean |
await(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for this future to be completed within the
specified time limit.
|
private boolean |
await0(long timeoutNanos,
boolean interruptable) |
Promise<V> |
awaitUninterruptibly()
Waits for this future to be completed without
interruption.
|
boolean |
awaitUninterruptibly(long timeoutMillis)
Waits for this future to be completed within the
specified time limit without interruption.
|
boolean |
awaitUninterruptibly(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for this future to be completed within the
specified time limit without interruption.
|
boolean |
cancel(boolean mayInterruptIfRunning)
If the cancellation was successful it will fail the future with an
CancellationException . |
java.lang.Throwable |
cause()
Returns the cause of the failed I/O operation if the I/O operation has
failed.
|
protected void |
checkDeadLock() |
private void |
checkNotifyWaiters() |
private void |
decWaiters() |
protected EventExecutor |
executor()
Get the executor used to notify listeners when this promise is complete.
|
V |
getNow()
Return the result without blocking.
|
private void |
incWaiters() |
boolean |
isCancellable()
returns
true if and only if the operation can be cancelled via Future.cancel(boolean) . |
boolean |
isCancelled() |
private static boolean |
isCancelled0(java.lang.Object result) |
boolean |
isDone() |
private static boolean |
isDone0(java.lang.Object result) |
boolean |
isSuccess()
Returns
true if and only if the I/O operation was completed
successfully. |
protected static void |
notifyListener(EventExecutor eventExecutor,
Future<?> future,
GenericFutureListener<?> listener)
Notify a listener that a future has completed.
|
private static void |
notifyListener0(Future future,
GenericFutureListener l) |
private void |
notifyListeners() |
private void |
notifyListeners0(DefaultFutureListeners listeners) |
private void |
notifyListenersNow() |
private static void |
notifyListenerWithStackOverFlowProtection(EventExecutor executor,
Future<?> future,
GenericFutureListener<?> listener)
The logic in this method should be identical to
notifyListeners() but
cannot share code because the listener(s) cannot be cached for an instance of DefaultPromise since the
listener(s) may be changed and is protected by a synchronized operation. |
private static void |
notifyProgressiveListener0(ProgressiveFuture future,
GenericProgressiveFutureListener l,
long progress,
long total) |
(package private) void |
notifyProgressiveListeners(long progress,
long total)
Notify all progressive listeners.
|
private static void |
notifyProgressiveListeners0(ProgressiveFuture<?> future,
GenericProgressiveFutureListener<?>[] listeners,
long progress,
long total) |
private java.lang.Object |
progressiveListeners()
|
Promise<V> |
removeListener(GenericFutureListener<? extends Future<? super V>> listener)
Removes the first occurrence of the specified listener from this future.
|
private void |
removeListener0(GenericFutureListener<? extends Future<? super V>> listener) |
Promise<V> |
removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Removes the first occurrence for each of the listeners from this future.
|
private void |
rethrowIfFailed() |
private static void |
safeExecute(EventExecutor executor,
java.lang.Runnable task) |
Promise<V> |
setFailure(java.lang.Throwable cause)
Marks this future as a failure and notifies all
listeners.
|
private boolean |
setFailure0(java.lang.Throwable cause) |
Promise<V> |
setSuccess(V result)
Marks this future as a success and notifies all
listeners.
|
private boolean |
setSuccess0(V result) |
boolean |
setUncancellable()
Make this future impossible to cancel.
|
private boolean |
setValue0(java.lang.Object objResult) |
Promise<V> |
sync()
Waits for this future until it is done, and rethrows the cause of the failure if this future
failed.
|
Promise<V> |
syncUninterruptibly()
Waits for this future until it is done, and rethrows the cause of the failure if this future
failed.
|
java.lang.String |
toString() |
protected java.lang.StringBuilder |
toStringBuilder() |
boolean |
tryFailure(java.lang.Throwable cause)
Marks this future as a failure and notifies all
listeners.
|
boolean |
trySuccess(V result)
Marks this future as a success and notifies all
listeners.
|
get, get
private static final InternalLogger logger
private static final InternalLogger rejectedExecutionLogger
private static final int MAX_LISTENER_STACK_DEPTH
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DefaultPromise,java.lang.Object> RESULT_UPDATER
private static final Signal SUCCESS
private static final Signal UNCANCELLABLE
private static final DefaultPromise.CauseHolder CANCELLATION_CAUSE_HOLDER
private volatile java.lang.Object result
private final EventExecutor executor
private java.lang.Object listeners
GenericFutureListener
or a DefaultFutureListeners
.
If null
, it means either 1) no listeners were added yet or 2) all listeners were notified.
Threading - synchronized(this). We must support adding listeners when there is no EventExecutor.private short waiters
private boolean notifyingListeners
public DefaultPromise(EventExecutor executor)
EventExecutor.newPromise()
to create a new promiseexecutor
- the EventExecutor
which is used to notify the promise once it is complete.
It is assumed this executor will protect against StackOverflowError
exceptions.
The executor may be used to avoid StackOverflowError
by executing a Runnable
if the stack
depth exceeds a threshold.protected DefaultPromise()
executor()
for expectations of the executor.public Promise<V> setSuccess(V result)
Promise
IllegalStateException
.setSuccess
in interface Promise<V>
public boolean trySuccess(V result)
Promise
trySuccess
in interface Promise<V>
true
if and only if successfully marked this future as
a success. Otherwise false
because this future is
already marked as either a success or a failure.public Promise<V> setFailure(java.lang.Throwable cause)
Promise
IllegalStateException
.setFailure
in interface Promise<V>
public boolean tryFailure(java.lang.Throwable cause)
Promise
tryFailure
in interface Promise<V>
true
if and only if successfully marked this future as
a failure. Otherwise false
because this future is
already marked as either a success or a failure.public boolean setUncancellable()
Promise
setUncancellable
in interface Promise<V>
true
if and only if successfully marked this future as uncancellable or it is already done
without being cancelled. false
if this future has been cancelled already.public boolean isSuccess()
Future
true
if and only if the I/O operation was completed
successfully.public boolean isCancellable()
Future
true
if and only if the operation can be cancelled via Future.cancel(boolean)
.public java.lang.Throwable cause()
Future
null
if succeeded or this future is not
completed yet.public Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener)
Future
addListener
in interface Promise<V>
public Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Future
addListeners
in interface Promise<V>
public Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener)
Future
removeListener
in interface Promise<V>
public Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Future
removeListeners
in interface Promise<V>
public Promise<V> await() throws java.lang.InterruptedException
Future
public Promise<V> awaitUninterruptibly()
Future
InterruptedException
and
discards it silently.awaitUninterruptibly
in interface Promise<V>
public boolean await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Future
true
if and only if the future was completed within
the specified time limitjava.lang.InterruptedException
- if the current thread was interruptedpublic boolean await(long timeoutMillis) throws java.lang.InterruptedException
Future
true
if and only if the future was completed within
the specified time limitjava.lang.InterruptedException
- if the current thread was interruptedpublic boolean awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)
Future
InterruptedException
and discards it silently.true
if and only if the future was completed within
the specified time limitpublic boolean awaitUninterruptibly(long timeoutMillis)
Future
InterruptedException
and discards it silently.true
if and only if the future was completed within
the specified time limitpublic V getNow()
Future
null
.
As it is possible that a null
value is used to mark the future as successful you also need to check
if the future is really done with Future.isDone()
and not relay on the returned null
value.public boolean cancel(boolean mayInterruptIfRunning)
Future
CancellationException
.cancel
in interface java.util.concurrent.Future<V>
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<V>
public boolean isDone()
isDone
in interface java.util.concurrent.Future<V>
public Promise<V> sync() throws java.lang.InterruptedException
Future
public Promise<V> syncUninterruptibly()
Future
syncUninterruptibly
in interface Promise<V>
public java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.StringBuilder toStringBuilder()
protected EventExecutor executor()
It is assumed this executor will protect against StackOverflowError
exceptions.
The executor may be used to avoid StackOverflowError
by executing a Runnable
if the stack
depth exceeds a threshold.
protected void checkDeadLock()
protected static void notifyListener(EventExecutor eventExecutor, Future<?> future, GenericFutureListener<?> listener)
This method has a fixed depth of MAX_LISTENER_STACK_DEPTH
that will limit recursion to prevent
StackOverflowError
and will stop notifying listeners added after this threshold is exceeded.
eventExecutor
- the executor to use to notify the listener listener
.future
- the future that is complete.listener
- the listener to notify.private void notifyListeners()
private static void notifyListenerWithStackOverFlowProtection(EventExecutor executor, Future<?> future, GenericFutureListener<?> listener)
notifyListeners()
but
cannot share code because the listener(s) cannot be cached for an instance of DefaultPromise
since the
listener(s) may be changed and is protected by a synchronized operation.private void notifyListenersNow()
private void notifyListeners0(DefaultFutureListeners listeners)
private static void notifyListener0(Future future, GenericFutureListener l)
private void addListener0(GenericFutureListener<? extends Future<? super V>> listener)
private void removeListener0(GenericFutureListener<? extends Future<? super V>> listener)
private boolean setSuccess0(V result)
private boolean setFailure0(java.lang.Throwable cause)
private boolean setValue0(java.lang.Object objResult)
private void checkNotifyWaiters()
private void incWaiters()
private void decWaiters()
private void rethrowIfFailed()
private boolean await0(long timeoutNanos, boolean interruptable) throws java.lang.InterruptedException
java.lang.InterruptedException
void notifyProgressiveListeners(long progress, long total)
No attempt is made to ensure notification order if multiple calls are made to this method before the original invocation completes.
This will do an iteration over all listeners to get all of type GenericProgressiveFutureListener
s.
progress
- the new progress.total
- the total progress.private java.lang.Object progressiveListeners()
private static void notifyProgressiveListeners0(ProgressiveFuture<?> future, GenericProgressiveFutureListener<?>[] listeners, long progress, long total)
private static void notifyProgressiveListener0(ProgressiveFuture future, GenericProgressiveFutureListener l, long progress, long total)
private static boolean isCancelled0(java.lang.Object result)
private static boolean isDone0(java.lang.Object result)
private static void safeExecute(EventExecutor executor, java.lang.Runnable task)