From bb53933b74456a1427715bd161812fd3b0418318 Mon Sep 17 00:00:00 2001 From: samskivert Date: Sun, 1 Feb 2009 22:51:24 +0000 Subject: [PATCH] Note when shutdown has been requested and refuse to accept further units from that time, rather than from the time that the shutdown is actually processed. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2525 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Invoker.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/java/com/samskivert/util/Invoker.java b/src/java/com/samskivert/util/Invoker.java index 5f7a204a..8d7a5a08 100644 --- a/src/java/com/samskivert/util/Invoker.java +++ b/src/java/com/samskivert/util/Invoker.java @@ -144,8 +144,8 @@ public class Invoker extends LoopingThread */ public void postUnit (Unit unit) { - if (!isRunning()) { - throw new IllegalStateException("Invoker has been shutdown"); + if (!shutdownRequested()) { + throw new IllegalStateException("Cannot post units to shutdown invoker."); } // note the time unit.queueStamp = System.currentTimeMillis(); @@ -211,6 +211,7 @@ public class Invoker extends LoopingThread @Override public void shutdown () { + _shutdownRequested = true; _queue.append(new Unit() { @Override public boolean invoke () { _running = false; @@ -231,6 +232,15 @@ public class Invoker extends LoopingThread _profileBucketCount = bucketCount; } + /** + * Returns true if {@link #shutdown} has been called. {@link #isRunning} may still return true + * until the shutdown unit is reached and processed by the invoker thread. + */ + protected boolean shutdownRequested () + { + return _shutdownRequested; + } + /** * Called before we process an invoker unit. * @@ -328,6 +338,10 @@ public class Invoker extends LoopingThread /** The duration of time after which we consider a unit to be delinquent and log a warning. */ protected static long _defaultLongThreshold = 500L; + /** True after {@link #shutdown} has been called but before the invoker has finished processing + * any remaining queued units. */ + protected volatile boolean _shutdownRequested; + /** Whether or not to track invoker unit performance. */ protected static final boolean PERF_TRACK = true; }