From 5457f286bc6a4d160dedd7b7f926de12b8a5a8bb Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 6 Oct 2006 20:58:19 +0000 Subject: [PATCH] We need a way to deal with the wacky situation where all threads created by an applet are forcibly destroyed when it is destroyed, even though the applet's class loader sticks around and it is started up again with the same classes (which have already been static initialized). Twisty maze of passages. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1939 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Interval.java | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/util/Interval.java b/src/java/com/samskivert/util/Interval.java index db7b52df..f6dadafe 100644 --- a/src/java/com/samskivert/util/Interval.java +++ b/src/java/com/samskivert/util/Interval.java @@ -46,6 +46,20 @@ public abstract class Interval public Interval getInterval (); } + /** + * Clears out the {@link Timer} currently being used to schedule + * intervals. This method is normally not needed as a single timer thread + * can be used for the lifetime of the VM, but in certain situations (in + * applets) the timer thread will be forcibly killed when the applet is + * destroyed but the classes will not be unloaded. In that case, the applet + * should call this method in its own destroy method. A new timer thread + * will then be created the next time an interval is scheduled. + */ + public static synchronized void shutdown () + { + _timer = null; + } + /** * Create a simple interval that does not use a RunQueue to run * the expired() method. @@ -104,9 +118,9 @@ public abstract class Interval _task = task; if (repeatDelay == 0L) { - _timer.schedule(task, initialDelay); + getTimer().schedule(task, initialDelay); } else { - _timer.scheduleAtFixedRate(task, initialDelay, repeatDelay); + getTimer().scheduleAtFixedRate(task, initialDelay, repeatDelay); } } @@ -153,6 +167,14 @@ public abstract class Interval } } + protected static synchronized Timer getTimer () + { + if (_timer == null) { + _timer = new Timer(/*JDK1.5 "samskivert Interval Timer",*/ true); + } + return _timer; + } + /** * The task that schedules actually runs the interval. */ @@ -194,7 +216,6 @@ public abstract class Interval /** The task that actually schedules our execution with the static Timer. */ protected volatile TimerTask _task; - /** The daemon timer used to schedule all Intervals. */ - protected static final Timer _timer = - new Timer(/*JDK1.5 "samskivert Interval Timer",*/ true); + /** The daemon timer used to schedule all intervals. */ + protected static Timer _timer; }