diff --git a/projects/samskivert/src/java/com/samskivert/util/BasicRunQueue.java b/projects/samskivert/src/java/com/samskivert/util/BasicRunQueue.java new file mode 100644 index 00000000..a64f0a38 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/BasicRunQueue.java @@ -0,0 +1,60 @@ +// +// $Id: RunQueue.java,v 1.1 2003/08/13 01:55:30 ray Exp $ + +package com.samskivert.util; + +import com.samskivert.Log; + +/** + * A very basic implementation of RunQueue for general purpose use. + */ +public class BasicRunQueue extends LoopingThread + implements RunQueue +{ + /** + * Construct a BasicRunQueue with a default Queue implementation. + */ + public BasicRunQueue () + { + super("RunQueue"); + _queue = new Queue(); + } + + // documentation inherited from interface + public void postRunnable (Runnable r) + { + _queue.append(r); + } + + // documentation inherited from interface + public boolean isDispatchThread () + { + return Thread.currentThread() == this; + } + + // documentation inherited + protected void iterate () + { + Runnable r = (Runnable) _queue.get(); + try { + r.run(); + + } catch (Throwable t) { + Log.warning("Runnable posted to RunQueue barfed."); + Log.logStackTrace(t); + } + } + + // documentation inherited + protected void kick () + { + postRunnable(new Runnable() { + public void run () { + // nothing + } + }); + } + + /** The queue of things to run. */ + protected Queue _queue; +} diff --git a/projects/samskivert/src/java/com/samskivert/util/Invoker.java b/projects/samskivert/src/java/com/samskivert/util/Invoker.java index a765fbf5..249fc87a 100644 --- a/projects/samskivert/src/java/com/samskivert/util/Invoker.java +++ b/projects/samskivert/src/java/com/samskivert/util/Invoker.java @@ -28,14 +28,6 @@ import com.samskivert.Log; */ public class Invoker extends LoopingThread { - /** An interface by which the invoker communicates back to the entity - * that manages the main thread. */ - public static interface ResultReceiver - { - /** Posts an unit that should be executed on the main thread. */ - public void postUnit (Runnable unit); - } - /** * The unit encapsulates a unit of executable code that will be run on * the invoker thread. It also provides facilities for additional code @@ -99,10 +91,10 @@ public class Invoker extends LoopingThread * Creates an invoker that will post results to the supplied result * receiver. */ - public Invoker (String name, ResultReceiver receiver) + public Invoker (String name, RunQueue resultReceiver) { super(name); - _receiver = receiver; + _receiver = resultReceiver; } /** @@ -131,7 +123,7 @@ public class Invoker extends LoopingThread if (unit.invoke()) { // if it returned true, we post it to the receiver thread // to invoke the result processing - _receiver.postUnit(unit); + _receiver.postRunnable(unit); } // track some performance metrics @@ -176,7 +168,7 @@ public class Invoker extends LoopingThread protected Queue _queue = new Queue(); /** The result receiver with which we're working. */ - protected ResultReceiver _receiver; + protected RunQueue _receiver; /** Tracks the counts of invocations by unit's class. */ protected HashMap _tracker = new HashMap(); diff --git a/projects/samskivert/src/java/com/samskivert/util/RunQueue.java b/projects/samskivert/src/java/com/samskivert/util/RunQueue.java index 41e09a10..451b96c3 100644 --- a/projects/samskivert/src/java/com/samskivert/util/RunQueue.java +++ b/projects/samskivert/src/java/com/samskivert/util/RunQueue.java @@ -1,51 +1,20 @@ // -// $Id: RunQueue.java,v 1.1 2003/08/13 01:55:30 ray Exp $ +// $Id$ package com.samskivert.util; -import com.samskivert.Log; - /** - * Used to serialize access to some resource. + * An interface for a service that queues up execution of Runnables. */ -public class RunQueue extends LoopingThread +public interface RunQueue { - public RunQueue () - { - super("RunQueue"); - } - /** - * Post a runnable unit for running on the run queue. + * Post the specified Runnable to be run on the RunQueue. */ - public void post (Runnable r) - { - _queue.append(r); - } + public void postRunnable (Runnable r); - // documentation inherited - protected void iterate () - { - Runnable r = (Runnable) _queue.get(); - try { - r.run(); - - } catch (Throwable t) { - Log.warning("Runnable posted to RunQueue barfed."); - Log.logStackTrace(t); - } - } - - // documentation inherited - protected void kick () - { - post(new Runnable() { - public void run () { - // nothing - } - }); - } - - /** The queue of things to run. */ - protected Queue _queue = new Queue(); + /** + * @return true if the calling thread is the RunQueue dispatch thread. + */ + public boolean isDispatchThread (); } diff --git a/projects/samskivert/src/java/com/samskivert/util/SerialExecutor.java b/projects/samskivert/src/java/com/samskivert/util/SerialExecutor.java index 5b27addd..87e4e822 100644 --- a/projects/samskivert/src/java/com/samskivert/util/SerialExecutor.java +++ b/projects/samskivert/src/java/com/samskivert/util/SerialExecutor.java @@ -45,7 +45,7 @@ public class SerialExecutor /** * Construct the SerialExecutor. */ - public SerialExecutor (Invoker.ResultReceiver receiver) + public SerialExecutor (RunQueue receiver) { _receiver = receiver; } @@ -105,7 +105,7 @@ public class SerialExecutor Log.logStackTrace(t); } - _receiver.postUnit(new Runnable() { + _receiver.postRunnable(new Runnable() { public void run () { try { _task.resultReceived(); @@ -123,7 +123,7 @@ public class SerialExecutor } /** The receiver to which we post a unit to process results. */ - protected Invoker.ResultReceiver _receiver; + protected RunQueue _receiver; /** True if there is a thread currently executing a task. */ protected boolean _executingNow = false;