1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
WatchDog.java
/**
*
*/
package Materiel.TTi;
import java.lang.reflect.Method;
/**
*
*/
public class WatchDog extends Thread {
private long _maxDelay;
private long _lastDate;
private boolean _isClosed;
private Object _anObject;
private String _aMethod;
/**
* delay en seconde
*
* @throws Exception
*/
public WatchDog(long aMaxDelay,Object anObject, String aMethod{
_maxDelay = aMaxDelay * 1000;
_lastDate = -1;
_isClosed = false;
this._anObject = anObject;
this._aMethod = aMethod;
start();
}
public void run() {
while (true) {
synchronized (this) {
try {
long d = System.currentTimeMillis();
if (_isClosed ||
((-1 != _lastDate) && ((d - _lastDate) > _maxDelay))){
try {
Method m = _anObject.getClass().getMethod(_aMethod,
null);
if (null != m)
m.invoke(_anObject, null);
} catch (Exception e) {}
_lastDate = -1;
}
if (_isClosed)
break;
if (-1 == _lastDate)
this.wait();
else
this.wait(_maxDelay - d + _lastDate);
} catch (Exception e) {}
}
}
}
public synchronized void reInit() {
_lastDate = System.currentTimeMillis();
this.notifyAll();
}
public synchronized void close() {
_isClosed = true;
this.notifyAll();
}
} |