From 7fb3c3354cf8f81b1bc3c15782e230c4d2b0d256 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Mon, 7 Dec 2009 21:23:18 +0000 Subject: [PATCH] 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 --- .../com/samskivert/util/SerialExecutor.java | 51 +++++++++++++++++-- .../util/tests/SerialExecutorTest.java | 15 +++++- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/java/com/samskivert/util/SerialExecutor.java b/src/java/com/samskivert/util/SerialExecutor.java index 2e4c1512..b8b99f24 100644 --- a/src/java/com/samskivert/util/SerialExecutor.java +++ b/src/java/com/samskivert/util/SerialExecutor.java @@ -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 _queue = new ArrayList(); } diff --git a/src/java/com/samskivert/util/tests/SerialExecutorTest.java b/src/java/com/samskivert/util/tests/SerialExecutorTest.java index 1b3d8336..95885e04 100644 --- a/src/java/com/samskivert/util/tests/SerialExecutorTest.java +++ b/src/java/com/samskivert/util/tests/SerialExecutorTest.java @@ -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;