V
- The result type returned by the future.public abstract class AbstractListenableFuture<V> extends java.lang.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 java.lang.Object |
completionListenerLock
Synchronization object that this class synchronizes on when retrieving or
modifying the set of registered listeners.
|
protected ListenerLockPolicy |
listenerLockPolicy
Lock policy used for completion listeners.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractListenableFuture()
Default constructor.
|
protected |
AbstractListenableFuture(ListenerLockPolicy listenerLockPolicy)
Initializes this class with the specified listener lock-policy and an
empty set of listeners.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addCompletionListener(FutureCompletionListener<? super V> listener)
Registers a completion listener with this future.
|
ListenerLockPolicy |
getListenerLockPolicy()
Returns the listener lock-policy used by 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 java.lang.Object completionListenerLock
protected final ListenerLockPolicy listenerLockPolicy
removeCompletionListener(FutureCompletionListener)
listener
method is called while a notification for the listener that shall be
removed is in progress.protected AbstractListenableFuture()
BLOCK
.
Child classes that want to set the ListenerLockPolicy
should use
the AbstractListenableFuture(ListenerLockPolicy)
constructor
instead.protected AbstractListenableFuture(ListenerLockPolicy listenerLockPolicy)
listenerLockPolicy
- listener lock-policy used by this future. The lock policy
defines the behavior of the
removeCompletionListener(FutureCompletionListener)
method.protected void notifyCompletionListeners()
notifyCompletionListener(FutureCompletionListener)
, so that
derived classes can take extra precautions (e.g. catch certain exceptions
thrown by a listener).java.lang.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 ListenerLockPolicy getListenerLockPolicy()
ListenableFuture
ListenableFuture.removeCompletionListener(FutureCompletionListener)
method when a
listener is removed while a notification of the same listener is in
progress.getListenerLockPolicy
in interface ListenableFuture<V>
public boolean removeCompletionListener(FutureCompletionListener<? super V> listener)
ListenableFuture
Removes the registration of a completion listener with this future. The exact behavior of this method depends on the listener lock-policy used by this future.
If the BLOCK
policy is in effect, the
listener is not going to receive a completion notification after this
method returns. If the IGNORE
policy is
in effect, the listener might still be notified after this method
returns. This may only happen if the future completes before this method
returns. If the REPORT
policy is in
effect, this method throws a ConcurrentNotificationException
if
the listener might still be notified. The listener is still removed,
despite the exception being thrown.
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.ListenableFuture.getListenerLockPolicy()
Copyright © 2014–2017 aquenos GmbH. All rights reserved.