From 6c69cd0b20bc6df74607532b543a746593e5af37 Mon Sep 17 00:00:00 2001 From: samskivert Date: Mon, 20 Jul 2009 22:13:26 +0000 Subject: [PATCH] Log a warning and cancel a repeating interval if it attempts to post itself to a RunQueue that has been shutdown. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2596 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Interval.java | 62 ++++++++++++++-------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/java/com/samskivert/util/Interval.java b/src/java/com/samskivert/util/Interval.java index 60857775..4ec09ec5 100644 --- a/src/java/com/samskivert/util/Interval.java +++ b/src/java/com/samskivert/util/Interval.java @@ -141,6 +141,10 @@ public abstract class Interval /** * Schedule the interval to execute repeatedly with fixed-rate scheduling between repeats, * with the same delay. Supersedes any previous schedule that this Interval may have had. + * + *

Note: if a repeating interval is scheduled to post itself to a RunQueue and the target + * RunQueue is shutdown when the interval expires, the interval will cancel itself and log a + * warning message.

*/ public final void schedule (long delay, boolean repeat) { @@ -149,8 +153,12 @@ public abstract class Interval /** * Schedule the interval to execute repeatedly with the specified initial delay and repeat - * delay with fixed-rate scheduling between repeats. Supersedes any previous schedule that - * this Interval may have had. + * delay with fixed-rate scheduling between repeats. Supersedes any previous schedule that this + * Interval may have had. + * + *

Note: if a repeating interval is scheduled to post itself to a RunQueue and the target + * RunQueue is shutdown when the interval expires, the interval will cancel itself and log a + * warning message.

*/ public final void schedule (long initialDelay, long repeatDelay) { @@ -161,6 +169,10 @@ public abstract class Interval * Schedule the interval to execute repeatedly with the specified initial delay and repeat * delay. Supersedes any previous schedule that this Interval may have had. * + *

Note: if a repeating interval is scheduled to post itself to a RunQueue and the target + * RunQueue is shutdown when the interval expires, the interval will cancel itself and log a + * warning message.

+ * * @param fixedRate - if true, this interval schedules repeated expirations using * {@link Timer#scheduleAtFixedRate(TimerTask, long, long)} ensuring that the number of * expired calls will match the amount of time elapsed. If false, it uses @@ -276,36 +288,42 @@ public abstract class Interval Interval ival = _interval; if (ival == null) { return; - - } else if (ival._runQueue == null) { + } + if (ival._runQueue == null) { ival.safelyExpire(this); + return; + } - } else { - if (_runner == null) { // lazy initialize _runner - _runner = new RunBuddy() { - public void run () { - Interval ival = _interval; - if (ival != null) { - ival.safelyExpire(IntervalTask.this); - } + if (_runner == null) { // lazy initialize _runner + _runner = new RunBuddy() { + public void run () { + Interval ival = _interval; + if (ival != null) { + ival.safelyExpire(IntervalTask.this); } + } + public Interval getInterval () { + return _interval; + } + @Override public String toString () { + Interval ival = _interval; + return (ival != null) ? ival.toString() : "(Interval was cancelled)"; + } + }; + } - public Interval getInterval () { - return _interval; - } - - @Override public String toString () { - Interval ival = _interval; - return (ival != null) ? ival.toString() : "(Interval was cancelled)"; - } - }; - } + if (ival._runQueue.isRunning()) { try { ival._runQueue.postRunnable(_runner); } catch (Exception e) { log.warning("Failed to execute interval on run-queue", "queue", ival._runQueue, "interval", ival, e); } + + } else { + log.warning("Interval posted to shutdown RunQueue. Cancelling.", + "queue", ival._runQueue, "interval", ival); + ival.cancel(); } }