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:
@@ -22,6 +22,8 @@ package com.samskivert.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
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.
|
||||
*/
|
||||
public class SerialExecutor
|
||||
implements 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;
|
||||
_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;
|
||||
|
||||
// let the task know that it timed out
|
||||
_receiver.postRunnable(new Runnable() {
|
||||
_receiver.execute(new Runnable() {
|
||||
public void run () {
|
||||
try {
|
||||
task.timedOut();
|
||||
@@ -191,7 +229,7 @@ public class SerialExecutor
|
||||
log.warning("Unit failed", t);
|
||||
}
|
||||
|
||||
_receiver.postRunnable(new Runnable() {
|
||||
_receiver.execute(new Runnable() {
|
||||
public void run () {
|
||||
try {
|
||||
task.resultReceived();
|
||||
@@ -207,11 +245,14 @@ public class SerialExecutor
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
protected boolean _executingNow = false;
|
||||
|
||||
/** A default timeout to use when executing simple Runnables. */
|
||||
protected long _runnableTimeout;
|
||||
|
||||
/** The queue of tasks to execute. */
|
||||
protected ArrayList<ExecutorTask> _queue = new ArrayList<ExecutorTask>();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
package com.samskivert.util.tests;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -31,7 +33,7 @@ import com.samskivert.util.SerialExecutor;
|
||||
* Tests the {@link SerialExecutor} class.
|
||||
*/
|
||||
public class SerialExecutorTest extends TestCase
|
||||
implements RunQueue
|
||||
implements Executor, RunQueue
|
||||
{
|
||||
public SerialExecutorTest ()
|
||||
{
|
||||
@@ -42,7 +44,7 @@ public class SerialExecutorTest extends TestCase
|
||||
@Override
|
||||
public void runTest ()
|
||||
{
|
||||
SerialExecutor executor = new SerialExecutor(this);
|
||||
SerialExecutor executor = new SerialExecutor((Executor)this);
|
||||
int added = 0;
|
||||
|
||||
// _sleeps++, _exits++, _results++
|
||||
@@ -88,16 +90,25 @@ public class SerialExecutorTest extends TestCase
|
||||
value == expected);
|
||||
}
|
||||
|
||||
// from Executor
|
||||
public void execute (Runnable command)
|
||||
{
|
||||
_queue.append(command);
|
||||
}
|
||||
|
||||
// from RunQueue
|
||||
public void postRunnable (Runnable r)
|
||||
{
|
||||
_queue.append(r);
|
||||
}
|
||||
|
||||
// from RunQueue
|
||||
public boolean isDispatchThread ()
|
||||
{
|
||||
return Thread.currentThread() == _main;
|
||||
}
|
||||
|
||||
// from RunQueue
|
||||
public boolean isRunning ()
|
||||
{
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user