Well, it's called "SerialExecutor", it would be a shame not to implement

the Executor interface.
- Also accept our "receiver" as an Executor, deprecating the old RunQueue
  constructor.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2671 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2009-12-07 21:23:18 +00:00
parent 50862df9c3
commit 7fb3c3354c
2 changed files with 59 additions and 7 deletions
@@ -22,6 +22,8 @@ package com.samskivert.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Executor;
import static com.samskivert.Log.log; import static com.samskivert.Log.log;
/** /**
@@ -32,6 +34,7 @@ import static com.samskivert.Log.log;
* that they will not block the eventual termination of the virtual machine. * that they will not block the eventual termination of the virtual machine.
*/ */
public class SerialExecutor public class SerialExecutor
implements Executor
{ {
/** /**
* A task to run in serial on the executor. * A task to run in serial on the executor.
@@ -76,11 +79,46 @@ public class SerialExecutor
} }
/** /**
* Construct the SerialExecutor. * Construct the SerialExecutor, using a 30-second default timeout for posted Runnables.
*/ */
public SerialExecutor (RunQueue receiver) public SerialExecutor (Executor receiver)
{
this(receiver, 30L * 1000L);
}
/**
* Construct the SerialExecutor, using a the specified timeout for posted Runnables.
*/
public SerialExecutor (Executor receiver, long runnableTimeout)
{ {
_receiver = receiver; _receiver = receiver;
_runnableTimeout = runnableTimeout;
}
/**
* Construct the SerialExecutor.
* @deprecated
*/
@Deprecated
public SerialExecutor (final RunQueue receiver)
{
this(new Executor() {
public void execute (Runnable r) {
receiver.postRunnable(r);
}
});
}
// from Executor
public void execute (final Runnable command)
{
addTask(new ExecutorTask() {
public boolean merge (ExecutorTask other) { return false; }
public long getTimeout () { return _runnableTimeout; }
public void executeTask () { command.run(); }
public void resultReceived () { /* nada */ }
public void timedOut () { /* nada */ }
});
} }
/** /**
@@ -157,7 +195,7 @@ public class SerialExecutor
_task = null; _task = null;
// let the task know that it timed out // let the task know that it timed out
_receiver.postRunnable(new Runnable() { _receiver.execute(new Runnable() {
public void run () { public void run () {
try { try {
task.timedOut(); task.timedOut();
@@ -191,7 +229,7 @@ public class SerialExecutor
log.warning("Unit failed", t); log.warning("Unit failed", t);
} }
_receiver.postRunnable(new Runnable() { _receiver.execute(new Runnable() {
public void run () { public void run () {
try { try {
task.resultReceived(); task.resultReceived();
@@ -207,11 +245,14 @@ 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 RunQueue _receiver; protected Executor _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;
/** A default timeout to use when executing simple Runnables. */
protected long _runnableTimeout;
/** The queue of tasks to execute. */ /** The queue of tasks to execute. */
protected ArrayList<ExecutorTask> _queue = new ArrayList<ExecutorTask>(); protected ArrayList<ExecutorTask> _queue = new ArrayList<ExecutorTask>();
} }
@@ -20,6 +20,8 @@
package com.samskivert.util.tests; package com.samskivert.util.tests;
import java.util.concurrent.Executor;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
@@ -31,7 +33,7 @@ import com.samskivert.util.SerialExecutor;
* Tests the {@link SerialExecutor} class. * Tests the {@link SerialExecutor} class.
*/ */
public class SerialExecutorTest extends TestCase public class SerialExecutorTest extends TestCase
implements RunQueue implements Executor, RunQueue
{ {
public SerialExecutorTest () public SerialExecutorTest ()
{ {
@@ -42,7 +44,7 @@ public class SerialExecutorTest extends TestCase
@Override @Override
public void runTest () public void runTest ()
{ {
SerialExecutor executor = new SerialExecutor(this); SerialExecutor executor = new SerialExecutor((Executor)this);
int added = 0; int added = 0;
// _sleeps++, _exits++, _results++ // _sleeps++, _exits++, _results++
@@ -88,16 +90,25 @@ public class SerialExecutorTest extends TestCase
value == expected); value == expected);
} }
// from Executor
public void execute (Runnable command)
{
_queue.append(command);
}
// from RunQueue
public void postRunnable (Runnable r) public void postRunnable (Runnable r)
{ {
_queue.append(r); _queue.append(r);
} }
// from RunQueue
public boolean isDispatchThread () public boolean isDispatchThread ()
{ {
return Thread.currentThread() == _main; return Thread.currentThread() == _main;
} }
// from RunQueue
public boolean isRunning () public boolean isRunning ()
{ {
return true; return true;