FileDocCategorySizeDatePackage
Watchdog.javaAPI DocApache Ant 1.703671Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.util

Watchdog

public class Watchdog extends Object implements Runnable
Generalization of ExecuteWatchdog
since
Ant 1.5
see
org.apache.tools.ant.taskdefs.ExecuteWatchdog

Fields Summary
private Vector
observers
private long
timeout
private volatile boolean
stopped
marked as volatile to stop the compiler caching values or (in java1.5+, reordering access)
public static final String
ERROR_INVALID_TIMEOUT
Error string. {@value}
Constructors Summary
public Watchdog(long timeout)
Constructor for Watchdog.

param
timeout the timeout to use in milliseconds (must be >= 1).


                        
       
        if (timeout < 1) {
            throw new IllegalArgumentException(ERROR_INVALID_TIMEOUT);
        }
        this.timeout = timeout;
    
Methods Summary
public voidaddTimeoutObserver(TimeoutObserver to)
Add a timeout observer.

param
to the timeout observer to add.

        //no need to synchronize, as Vector is always synchronized
        observers.addElement(to);
    
protected final voidfireTimeoutOccured()
Inform the observers that a timeout has occurred. This happens in the watchdog thread.

        Enumeration e = observers.elements();
        while (e.hasMoreElements()) {
            ((TimeoutObserver) e.nextElement()).timeoutOccured(this);
        }
    
public voidremoveTimeoutObserver(TimeoutObserver to)
Remove a timeout observer.

param
to the timeout observer to remove.

        //no need to synchronize, as Vector is always synchronized
        observers.removeElement(to);
    
public synchronized voidrun()
The run method of the watch dog thread. This simply does a wait for the timeout time, and if the stop flag has not been set when the wait has returned or has been interrupted, the watch dog listeners are informed.

        final long until = System.currentTimeMillis() + timeout;
        long now;
        while (!stopped && until > (now = System.currentTimeMillis())) {
            try {
                wait(until - now);
            } catch (InterruptedException e) {
                // Ignore exception
            }
        }
        if (!stopped) {
            fireTimeoutOccured();
        }
    
public synchronized voidstart()
Start the watch dog.

        stopped = false;
        Thread t = new Thread(this, "WATCHDOG");
        t.setDaemon(true);
        t.start();
    
public synchronized voidstop()
Stop the watch dog.

        stopped = true;
        notifyAll();