public enum ListenerLockPolicy extends java.lang.Enum<ListenerLockPolicy>
Policy applied when dealing with listener objects.
When dealing with listeners that can be removed (unregistered) there is an inherent conflict of interest: A listener might be called to be notified of an event while another thread tries to remove this exact listener. In general there are three different ways how one can deal with this situation:
BLOCK
value. While
this policy is the easiest to understand, as it provides strong consistency
guarantees, it is prone to potential deadlocks (see below).IGNORE
value.ConcurrentNotificationException
.
This policy is represented by the REPORT
value. Like in case of the
IGNORE
policy, the listener is still removed.BLOCK
policy
When using the BLOCK
policy, the code calling the listener has to
hold a lock (lock A) while calling the listener. The code removing the
listener has to acquire the same lock. Fundamentally, this is not dangerous,
but danger might arise if the listener also acquires a lock (lock B). Now, if
the code removing the listener holds the same lock (lock B) while trying to
remove the listener, there is the risk of a deadlock. This happens when the
code trying to remove the listener acquires the external lock (lock B) while
another thread acquires the internal lock (lock A) in order to notify the
listener. Now the thread trying to remove the listener cannot continue
because it has to acquire lock A in order to do so, but this lock is held by
the thread calling the listener. The thread calling the listener cannot
continue either, because the listener tries to acquire lock B, which is
already held by the thread trying to remove the listener.
This means that when using the BLOCK
policy, it is very important to
not acquire a lock in a listener, that might also be held by code calling the
remove method. If such a situation is unavoidable or not in the sphere of
influence of the developer (e.g. because a listener has to be removed in
response to a request made by some external code potentially holding a lock
and some external code potentially acquire the same lock has to be called
from a listener), the IGNORE
or REPORT
policy should be
used instead.
Enum Constant and Description |
---|
BLOCK
Wait for an internal lock when removing a listener.
|
IGNORE
Do not wait for the internal lock when removing a listener.
|
REPORT
Do not wait for the internal lock when removing a listener, but report
the problem.
|
Modifier and Type | Method and Description |
---|---|
static ListenerLockPolicy |
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.
|
static ListenerLockPolicy[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final ListenerLockPolicy BLOCK
public static final ListenerLockPolicy IGNORE
REPORT
policy, but unlike that policy does not notify the
caller of the fact that the listener might still be called one more time.public static final ListenerLockPolicy REPORT
ConcurrentNotificationException
in order to notify the calling
code of this situation. This policy is similar to the IGNORE
policy, but unlike this policy, the IGNORE
policy does not cause
an exception and thus the calling code is not notified.public static ListenerLockPolicy[] values()
for (ListenerLockPolicy c : ListenerLockPolicy.values()) System.out.println(c);
public static ListenerLockPolicy valueOf(java.lang.String name)
name
- the name of the enum constant to be returned.java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is nullCopyright © 2014–2019 aquenos GmbH. All rights reserved.