|
JThreadKitTM v1.1.0 ( public members only)
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.jthreadkit.ListenerTool
Used to manage listeners (or observers). A class that needs to
have listeners added and removed can use a private instance of
of ListenerTool to help manage the task.
The following example shows how an instance of ListenerTool
can be used to manage listeners of the hypothetical type
SomeListener:
|
public interface SomeListener {
void messageReceived(String msg);
}
|
lockObject:
|
private |
This class is marked as final to prohibit subclasses. Instead
of extending this class, an instance of it should be referred to by
a member variable.
NOTE: This class is multithread-safe. All of the methods
synchronize on the lock object returned by getLockObject() to control concurrency.
| Inner Class Summary | |
static interface |
ListenerTool.Callback
Used to allow callbacks during notification. |
| Constructor Summary | |
ListenerTool(ListenerTool.Callback callback)
Creates a listener manager with the specified callback. |
|
ListenerTool(ListenerTool.Callback callback,
Object lock)
Creates a listener manager with the specified callback, lock object, no recursive notifications. |
|
ListenerTool(ListenerTool.Callback callback,
Object lock,
boolean allowRecursiveNotification)
Creates a listener manager with the specified callback, lock object, and recursive notification option. |
|
| Method Summary | |
boolean |
addListener(Object listener)
Adds the specified listener to the list. |
int |
addListeners(Object[] listenerList)
Adds all of the listeners to the current list. |
int |
getListenerCount()
Returns the number of listeners currently registered. |
Object |
getLockObject()
Returns the reference to the object that is synchronized on by other methods in the implementing class. |
void |
notifyListeners(Object data)
Notifies each of the registered listeners passing the specified data to each one. |
int |
removeAllListeners()
Removes all of the listeners in the list. |
boolean |
removeListener(Object listener)
Removes the specified listener from the list. |
String |
toString()
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public ListenerTool(ListenerTool.Callback callback,
Object lock,
boolean allowRecursiveNotification)
callback - the object to notify with each listenerlock - the alternative object to lock on during adding, removing,
and notifications. If null is passed in,
locking will occur on this instance.allowRecursiveNotification - if false (most common
case), recursive notifications will be squelched. If
true, then notifyListeners can be
called recursively.
public ListenerTool(ListenerTool.Callback callback,
Object lock)
|
this(callback, lock, false); |
ListenerTool(ListenerTool.Callback, Object, boolean)public ListenerTool(ListenerTool.Callback callback)
|
this(callback, null, false); |
ListenerTool(ListenerTool.Callback, Object, boolean)| Method Detail |
public Object getLockObject()
AccessibleLock
This reference can be used in synchronized blocks
to keep other threads from sneaking in between methods calls
like this:
|
obj = //...
synchronized ( obj.getLockObject() ) {
obj.methodA();
obj.methodB();
}
|
getLockObject in interface AccessibleLockcom.jthreadkit.AccessibleLockpublic int getListenerCount()
public boolean addListener(Object listener)
== on the two
object references).listener - the listener to add to the current list.true if the listener was added,
false if not added (this was a duplicate listener).public int addListeners(Object[] listenerList)
listenerList - the listeners to add.listenerList.length if there were any duplicates.public boolean removeListener(Object listener)
listener - the listener to remove (using ==
comparisions to locate it).true if the listener was found and removed,
false if the listener was not found in this list.public int removeAllListeners()
public void notifyListeners(Object data)
data to each one. During the entire notification
duration, the lock is held (see getLockObject().
If you don't want to send any specific information to each listener
you can pass in null for data.
The notifyListener(Object, Object) method on ListenerTool.Callback
is invoked once for each listener.
If recursive notifications are not allowed (this is specified
during construction), recursive calls are silently ignored.
Although it is most common to prohibit the resursion, it can
be enabled during construction (see this constructor).
Note
that only the thread already inside this method could re-enter it
since it is holding the lock--other threads are blocked waiting
to get exclusive access to the lock.
data - the message to pass to each listener. Can be
null.public String toString()
toString in class Object
|
JThreadKitTM v1.1.0 ( public members only)
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||