|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.logicalcobwebs.proxool.util.AbstractListenerContainer
Implementation of ListenerContainerIF
that uses a reads/write lock to handle concurrency in a safe and
fast way.
The registered listeners are offered to subclasses through the protected getListenerIterator()
method. This
method aquires a read lock before it returns the iterator, but it is the subclass responsibility to
release this lock by calling the releaseReadLock()
method. Failing to do this will prevent any more
listeneres to be added or removed. Your code sould look like this:
Iterator listenerIterator = null;
try {
listenerIterator = getListenerIterator();
if (listenerIterator != null) {
// ... Iterate through the listeners and notify them
}
} catch (InterruptedException e) {
LOG.error("Tried to aquire read lock for " + MyClass.class.getName()
+ " iterator but was interrupted.");
} finally {
releaseReadLock();
}
Field Summary | |
private java.util.List |
listeners
|
(package private) static Log |
LOG
|
private WriterPreferenceReadWriteLock |
readWriteLock
|
Constructor Summary | |
AbstractListenerContainer()
|
Method Summary | |
void |
addListener(java.lang.Object listener)
Add a listener to this container. |
protected java.util.Iterator |
getListenerIterator()
Get an iterator containing the listeners in this container. |
boolean |
isEmpty()
Get wether this container is empty or not. |
protected void |
releaseReadLock()
Release the read lock aquired by the getListenerIterator() method. |
boolean |
removeListener(java.lang.Object listener)
Remove a listener from this container. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
static final Log LOG
private java.util.List listeners
private WriterPreferenceReadWriteLock readWriteLock
Constructor Detail |
public AbstractListenerContainer()
Method Detail |
public void addListener(java.lang.Object listener)
ListenerContainerIF
addListener
in interface ListenerContainerIF
listener
- the listener to add.ListenerContainerIF.addListener(Object)
public boolean removeListener(java.lang.Object listener)
ListenerContainerIF
removeListener
in interface ListenerContainerIF
listener
- the listener to be removed.
ListenerContainerIF.removeListener(Object)
protected java.util.Iterator getListenerIterator() throws java.lang.InterruptedException
null
if this container is empty.
Important: See class documentation regarding releasing of read lock.
null
if it is empty.
java.lang.InterruptedException
- if the read lock can't be obtained.protected void releaseReadLock()
getListenerIterator()
method.
public boolean isEmpty()
ListenerContainerIF
isEmpty
in interface ListenerContainerIF
ListenerContainerIF.isEmpty()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |