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; }