FileDocCategorySizeDatePackage
ChangeMonitor.javaAPI DocExample2475Sat Feb 01 07:43:02 GMT 1997imaginary.gui

ChangeMonitor

public class ChangeMonitor extends Thread
This class manages a queue of ChangeObserver objects. Each time a change occurs to a Persistent object, the Persistent calls update() in all observers. Because RMI blocks during remote method calls, you need to do any display changes in a new thread. So update() calls ChangeMonitor.postChange() which adds the object to the stack. A separate thread then periodically checks the stack and calls observeChange() in each of the ChangeObserver instances on the stack.

Fields Summary
private static ChangeMonitor
monitor
private Vector
queue
Constructors Summary
public ChangeMonitor()
Constructs a new monitor thread that maintains the queue.


                  
      
        super();
        setDaemon(true);
        start();
    
Methods Summary
private voidpost(ChangeObserver observer)

        synchronized(queue) {
            queue.addElement(observer);
        }
    
public static voidpostChange(ChangeObserver observer)
Adds a ChangeObserver object onto the queue.

param
observer the observer to add to the stack


                         
         
        ChangeMonitor.monitor.post(observer);
    
public voidrun()
Grabs the entire queue in an enumeration and calls observeChange() in each one.

see
imaginary.gui.ChangeObserver#observeChange

        while( true ) {
            Enumeration elems;

            // Grab a queue enumeration quickly and release
            // the synchronization.
            synchronized(queue) {
                if( !queue.isEmpty() ) {
                    elems = queue.elements();
                    queue = new Vector();
                }
                else {
                    elems = null;
                }
            }
            if( elems != null ) {
                while( elems.hasMoreElements() ) {
                    ChangeObserver o = (ChangeObserver)elems.nextElement();

                    o.observeChange();
                }
            }
            try { Thread.sleep(500); }
            catch( InterruptedException e ) { }
        }