Applications often need to monitor the state of the environment in which they run. Monitor threads provide a framework for monitoring changes in the status of part of a system that cannot be monitored by listening for events.
Applications often need to monitor the state of the environment in which they run. For example, an application might need to know whether a server is on-line and receiving requests from clients. The application might provide a user interface, such as a server icon, that represents the current status of the server: the icon is enabled when the server is on-line and disabled when it is off-line.
Another example is an application that needs to monitor the contents of a directory. The application might implement real-time active loading of extensions: users copy extension files into an extension directory and the application detects the new files and loads the new extensions automatically.
If the object to be monitored is part of the application, the application can listen for events generated by the object to notify interested clients that it has changed. If the object to be monitored is not part of the application, or does not generate events, another solution is needed.
Monitor threads run in a cycle of monitoring and sleeping for a specified amount of time. Monitor threads are implemented by the
MonitorThread class. The
MonitorThread class implements the monitoring loop: the thread sleeps for a specified period of time, calls its
void monitor() method and resumes sleeping.
MonitorThread class is abstract and sub-classes must extend it to override the
monitor() method. The contract of the
monitor() method is that when called, it should check whatever it is designed to check and generate an appropriate event that will be sent to registered event listeners.
The amount of time the thread sleeps for determines the time between calling the
monitor() method. This interval is application specific and is heavily influenced by the cost of performing the monitoring operation.
MonitorThread class provides methods for adding and removing event listeners. Event listener objects implement the
MonitorThreadEventListener interface, which provides a basic implementation for monitor thread events. The
monitorChanged() method of the
MonitorThreadEventListener interface is called to indicate that the state of the object being monitored has changed. The
MonitorThreadEventListener interface can be extended to provide application specific event notification methods.
MonitorThread class has three methods for controlling the state of a monitor thread:
suspendMonitor() pauses the monitor thread;
resumeMonitor() reactivates a paused monitor thread; and
stopMonitor() stops a monitor thread running (stopped monitor threads cannot be resumed).
The following diagram illustrates the class diagram of the
MonitorThread class and two example monitor thread classes. The
monitor() method of the
ServerMonitor class polls a specified server to check its availability. The
monitor() method of the
DirectoryMonitor class checks the names and number of files in a specified directory to determine whether the contents of the directory has changed.
DirectoryMonitorThreadClient objects listen for the events generated by the sub-classes of the