From 24f1fb611a8b6e5d44007b5caf2bc89c4c39b561 Mon Sep 17 00:00:00 2001 From: samskivert Date: Sat, 8 Nov 2008 00:05:57 +0000 Subject: [PATCH] Added isRunning() to RunQueue, made BasicRunQueue work both in separate-thread mode and in just-call-run()-yourself mode. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2473 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/BasicRunQueue.java | 21 +++++++++++---- src/java/com/samskivert/util/RunQueue.java | 27 ++++++++++++------- .../util/tests/SerialExecutorTest.java | 5 ++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/java/com/samskivert/util/BasicRunQueue.java b/src/java/com/samskivert/util/BasicRunQueue.java index 74f35539..7871b006 100644 --- a/src/java/com/samskivert/util/BasicRunQueue.java +++ b/src/java/com/samskivert/util/BasicRunQueue.java @@ -45,19 +45,26 @@ public class BasicRunQueue extends LoopingThread _queue = new Queue(); } - // documentation inherited from interface + // from interface RunQueue public void postRunnable (Runnable r) { _queue.append(r); } - // documentation inherited from interface + // from interface RunQueue public boolean isDispatchThread () { - return Thread.currentThread() == this; + return Thread.currentThread() == _dispatcher; } - @Override + @Override // from LoopingThread + protected void willStart () + { + super.willStart(); + _dispatcher = Thread.currentThread(); + } + + @Override // from LoopingThread protected void iterate () { Runnable r = _queue.get(); @@ -69,7 +76,7 @@ public class BasicRunQueue extends LoopingThread } } - @Override + @Override // from LoopingThread protected void kick () { postRunnable(new Runnable() { @@ -81,4 +88,8 @@ public class BasicRunQueue extends LoopingThread /** The queue of things to run. */ protected Queue _queue; + + /** Our dispatcher thread (may == this or may be something else if we're being used directly + * rather than in separate thread mode). */ + protected Thread _dispatcher; } diff --git a/src/java/com/samskivert/util/RunQueue.java b/src/java/com/samskivert/util/RunQueue.java index dfeecd6c..f211c688 100644 --- a/src/java/com/samskivert/util/RunQueue.java +++ b/src/java/com/samskivert/util/RunQueue.java @@ -29,22 +29,29 @@ public interface RunQueue { /** A useful RunQueue that uses the AWT dispatch thread. */ public static final RunQueue AWT = new RunQueue() { - public void postRunnable (Runnable r) { - EventQueue.invokeLater(r); - } - - public boolean isDispatchThread () { - return EventQueue.isDispatchThread(); - } - }; + public void postRunnable (Runnable r) { + EventQueue.invokeLater(r); + } + public boolean isDispatchThread () { + return EventQueue.isDispatchThread(); + } + public boolean isRunning () { + return true; + } + }; /** * Post the specified Runnable to be run on the RunQueue. */ - public void postRunnable (Runnable r); + void postRunnable (Runnable r); /** * @return true if the calling thread is the RunQueue dispatch thread. */ - public boolean isDispatchThread (); + boolean isDispatchThread (); + + /** + * @return true if this run queue is still processing runnables, false if it has been shutdown. + */ + boolean isRunning (); } diff --git a/src/java/com/samskivert/util/tests/SerialExecutorTest.java b/src/java/com/samskivert/util/tests/SerialExecutorTest.java index f3ab28cb..1b3d8336 100644 --- a/src/java/com/samskivert/util/tests/SerialExecutorTest.java +++ b/src/java/com/samskivert/util/tests/SerialExecutorTest.java @@ -98,6 +98,11 @@ public class SerialExecutorTest extends TestCase return Thread.currentThread() == _main; } + public boolean isRunning () + { + return true; + } + public static Test suite () { return new SerialExecutorTest();