V
- The result type returned by the future.public abstract class AbstractListenableFuture<V> extends Object implements ListenableFuture<V>
Abstract base implementation of a ListenableFuture
.
This implementation stores the registered listeners in a HashSet
and
provides a notifyCompletionListeners()
method that should be invoked
by derived classes.
Derived classes can also override the
notifyCompletionListener(FutureCompletionListener)
method if they
want to modify the behavior how listeners are called.
Modifier and Type | Field and Description |
---|---|
protected Object |
completionListenerLock
Synchronization object that this class synchronizes on when modifying the
set of registered listeners or triggering the listeners from
notifyCompletionListeners() . |
Modifier | Constructor and Description |
---|---|
protected |
AbstractListenableFuture()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addCompletionListener(FutureCompletionListener<? super V> listener)
Registers a completion listener with this future.
|
protected void |
notifyCompletionListener(FutureCompletionListener<? super V> listener)
Notifies a completion listener.
|
protected void |
notifyCompletionListeners()
Notifies all currently registered completion listeners.
|
boolean |
removeCompletionListener(FutureCompletionListener<? super V> listener)
Removes the registration of a completion listener with this future.
|
protected final Object completionListenerLock
notifyCompletionListeners()
.protected AbstractListenableFuture()
protected void notifyCompletionListeners()
notifyCompletionListener(FutureCompletionListener)
, so that
derived classes can take extra precautions (e.g. catch certain exceptions
thrown by a listener).IllegalStateException
- if this method has already been called and thus the listeners
have already been notified or if this future has not
completed yet (Future.isDone()
returns false
).protected void notifyCompletionListener(FutureCompletionListener<? super V> listener)
listener
- the listener that should be notified. Notification should
happen by calling the listeners
completed()
method, passing a reference to this future
instance.public boolean addCompletionListener(FutureCompletionListener<? super V> listener)
ListenableFuture
Registers a completion listener with this future. This listener is
notified when the future completes (Future.isDone()
returns
true
). If the future completes before the listener is
registered, the listener is notified immediately while being added.
If the specified listener is already registered with this future, no
action is taken and this method returns false
.
Important note: Typically, the listener is executed by
the thread that sets the future's value. For this reason, it is important
that the listener does not block or perform any long-running operations.
Instead, the listener should delegate any such operation to a different
thread (e.g. using an Executor
). Any violation of this rule might
result in unexpected or unpredictable behavior. In particular, a blocking
listener might cause a dead-lock, which results in the whole application
being stalled indefinitely.
addCompletionListener
in interface ListenableFuture<V>
listener
- listener to be registered with this future.true
if the listener has been added,
false
if it has already been added earlier.public boolean removeCompletionListener(FutureCompletionListener<? super V> listener)
ListenableFuture
Removes the registration of a completion listener with this future. After removing, the listener is not going to be notified when the future completes.
If the specified listener is not currently registered with this future,
no action is taken and this method returns false
.
removeCompletionListener
in interface ListenableFuture<V>
listener
- listener to be unregistered from this future.true
if the listener has been removed,
false
if it has already been removed earlier of if
has never been added.Copyright © 2014–2016 aquenos GmbH. All rights reserved.