2008-12-30 16:43:37 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
2008-12-31 11:54:44 +00:00
|
|
|
import java.util.concurrent.ScheduledFuture;
|
2008-12-30 16:43:37 +00:00
|
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class Timer implements Runnable {
|
|
|
|
|
|
|
|
private final ScheduledThreadPoolExecutor executor;
|
|
|
|
|
2008-12-31 11:54:44 +00:00
|
|
|
private ScheduledFuture<?> scheduledFuture;
|
2008-12-30 16:43:37 +00:00
|
|
|
private Thread shutdownHook;
|
|
|
|
|
|
|
|
|
|
|
|
public Timer() {
|
|
|
|
executor = new ScheduledThreadPoolExecutor(1);
|
|
|
|
executor.setKeepAliveTime(200, TimeUnit.MILLISECONDS);
|
|
|
|
executor.allowCoreThreadTimeOut(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized void set(long delay, TimeUnit unit, boolean runBeforeShutdown) {
|
|
|
|
removeScheduledFuture();
|
|
|
|
|
|
|
|
Runnable r = this;
|
|
|
|
|
|
|
|
if (runBeforeShutdown) {
|
|
|
|
addShutdownHook();
|
|
|
|
|
|
|
|
// remove shutdown hook after execution
|
|
|
|
r = new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Timer.this.run();
|
|
|
|
} finally {
|
|
|
|
removeShutdownHook();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// remove existing shutdown hook, if any
|
|
|
|
removeShutdownHook();
|
|
|
|
}
|
|
|
|
|
2008-12-31 11:54:44 +00:00
|
|
|
scheduledFuture = executor.schedule(r, delay, unit);
|
2008-12-30 16:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized void cancel() {
|
|
|
|
removeScheduledFuture();
|
|
|
|
removeShutdownHook();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private synchronized void removeScheduledFuture() {
|
|
|
|
if (scheduledFuture != null) {
|
|
|
|
try {
|
2008-12-31 11:54:44 +00:00
|
|
|
scheduledFuture.cancel(true);
|
|
|
|
executor.purge();
|
2008-12-30 16:43:37 +00:00
|
|
|
} finally {
|
|
|
|
scheduledFuture = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private synchronized void addShutdownHook() {
|
|
|
|
if (shutdownHook == null) {
|
|
|
|
shutdownHook = new Thread(this);
|
|
|
|
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private synchronized void removeShutdownHook() {
|
|
|
|
if (shutdownHook != null) {
|
|
|
|
try {
|
|
|
|
if (shutdownHook != Thread.currentThread()) {
|
|
|
|
// can't remove shutdown hooks anymore, once runtime is shutting down,
|
|
|
|
// so don't remove the shutdown hook, if we are running on the shutdown hook
|
|
|
|
Runtime.getRuntime().removeShutdownHook(shutdownHook);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
shutdownHook = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|