This class is a thread safe list that is designed for storing lists of listeners.
The implementation is optimized for minimal memory footprint, frequent reads
and infrequent writes. Modification of the list is synchronized and relatively
expensive, while accessing the listeners is very fast. Readers are given access
to the underlying array data structure for reading, with the trust that they will
not modify the underlying array.
Use the getListeners method when notifying listeners. The recommended
code sequence for notifying all registered listeners of say,
FooListener.eventHappened, is:
Object[] listeners = myListenerList.getListeners();
for (int i = 0; i < listeners.length; ++i) {
((FooListener) listeners[i]).eventHappened(event);
}
A listener list handles the same listener being added multiple times, and tolerates removal of listeners that are the same as other listeners in the list. For this purpose, listeners can be compared with each other using either equality or identity, as specified in the list constructor.
Use the
getListenersmethod when notifying listeners. The recommended code sequence for notifying all registered listeners of say,FooListener.eventHappened, is:Object[] listeners = myListenerList.getListeners(); for (int i = 0; i < listeners.length; ++i) { ((FooListener) listeners[i]).eventHappened(event); }This class can be used without OSGi running.