Changed RunQueue to be called BasicRunQueue.

RunQueue is now an interface that is very simple- it serially runs Runnables.
Removed Invoker.ResultReceiver, the invoker now takes a RunQueue.
The method to post a Runnable to RunQueue is called postRunnable(), rather
than the legacy postUnit(). postUnit() is the method name of the Invoker
to post Invoker.Unit objects, and I just figured it was better to keep those
distinct, since an Invoker.Unit is also a Runnable, it could be very easy
to post an Invoker.Unit to the wrong thing if not careful.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1553 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2004-12-22 19:17:06 +00:00
parent f9b876998d
commit 15bff32e71
4 changed files with 76 additions and 55 deletions
@@ -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;
}
@@ -28,14 +28,6 @@ import com.samskivert.Log;
*/ */
public class Invoker extends LoopingThread 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 unit encapsulates a unit of executable code that will be run on
* the invoker thread. It also provides facilities for additional code * 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 * Creates an invoker that will post results to the supplied result
* receiver. * receiver.
*/ */
public Invoker (String name, ResultReceiver receiver) public Invoker (String name, RunQueue resultReceiver)
{ {
super(name); super(name);
_receiver = receiver; _receiver = resultReceiver;
} }
/** /**
@@ -131,7 +123,7 @@ public class Invoker extends LoopingThread
if (unit.invoke()) { if (unit.invoke()) {
// if it returned true, we post it to the receiver thread // if it returned true, we post it to the receiver thread
// to invoke the result processing // to invoke the result processing
_receiver.postUnit(unit); _receiver.postRunnable(unit);
} }
// track some performance metrics // track some performance metrics
@@ -176,7 +168,7 @@ public class Invoker extends LoopingThread
protected Queue _queue = new Queue(); protected Queue _queue = new Queue();
/** The result receiver with which we're working. */ /** The result receiver with which we're working. */
protected ResultReceiver _receiver; protected RunQueue _receiver;
/** Tracks the counts of invocations by unit's class. */ /** Tracks the counts of invocations by unit's class. */
protected HashMap _tracker = new HashMap(); protected HashMap _tracker = new HashMap();
@@ -1,51 +1,20 @@
// //
// $Id: RunQueue.java,v 1.1 2003/08/13 01:55:30 ray Exp $ // $Id$
package com.samskivert.util; 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 () /**
{ * Post the specified Runnable to be run on the RunQueue.
super("RunQueue"); */
} public void postRunnable (Runnable r);
/** /**
* Post a runnable unit for running on the run queue. * @return true if the calling thread is the RunQueue dispatch thread.
*/ */
public void post (Runnable r) public boolean isDispatchThread ();
{
_queue.append(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();
} }
@@ -45,7 +45,7 @@ public class SerialExecutor
/** /**
* Construct the SerialExecutor. * Construct the SerialExecutor.
*/ */
public SerialExecutor (Invoker.ResultReceiver receiver) public SerialExecutor (RunQueue receiver)
{ {
_receiver = receiver; _receiver = receiver;
} }
@@ -105,7 +105,7 @@ public class SerialExecutor
Log.logStackTrace(t); Log.logStackTrace(t);
} }
_receiver.postUnit(new Runnable() { _receiver.postRunnable(new Runnable() {
public void run () { public void run () {
try { try {
_task.resultReceived(); _task.resultReceived();
@@ -123,7 +123,7 @@ public class SerialExecutor
} }
/** The receiver to which we post a unit to process results. */ /** 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. */ /** True if there is a thread currently executing a task. */
protected boolean _executingNow = false; protected boolean _executingNow = false;