@@ -42,36 +42,18 @@ interface CancellableRunnable extends Runnable {
4242 * be run in parallel.
4343 */
4444final class SequentialExecutorService {
45- private static final Logger logger = Logger .getLogger (SequentialExecutorService .class .getName ());
4645
47- private final CallbackExecutor callbackExecutor ;
48- private final AutoExecutor autoExecutor ;
49-
50- SequentialExecutorService (Executor executor ) {
51- this .callbackExecutor = new CallbackExecutor (executor );
52- this .autoExecutor = new AutoExecutor (executor );
53- }
54-
55- /**
56- * Runs asynchronous {@code Callable} tasks sequentially. If one of the tasks fails, other tasks
57- * with the same key that have not been executed will be cancelled.
58- */
59- <T > ApiFuture <T > submit (final String key , final Callable <ApiFuture <T >> callable ) {
60- return callbackExecutor .submit (key , callable );
61- }
62-
63- /** Runs synchronous {@code Runnable} tasks sequentially. */
64- void submit (String key , Runnable runnable ) {
65- autoExecutor .execute (key , runnable );
46+ // This class is not directly usable.
47+ private SequentialExecutorService () {
6648 }
6749
6850 /**
69- * Internal implementation of SequentialExecutorService. Takes a serial stream of string keys and
51+ * This Executor takes a serial stream of string keys and
7052 * {@code Runnable} tasks, and runs the tasks with the same key sequentially. Tasks with the same
7153 * key will be run only when its predecessor has been completed while tasks with different keys
7254 * can be run in parallel.
7355 */
74- abstract static class SequentialExecutor {
56+ private abstract static class SequentialExecutor {
7557 // Maps keys to tasks.
7658 protected final Map <String , Deque <Runnable >> tasksByKey ;
7759 protected final Executor executor ;
@@ -81,7 +63,7 @@ private SequentialExecutor(Executor executor) {
8163 this .tasksByKey = new HashMap <>();
8264 }
8365
84- void execute (final String key , Runnable task ) {
66+ protected void execute (final String key , Runnable task ) {
8567 Deque <Runnable > newTasks ;
8668 synchronized (tasksByKey ) {
8769 newTasks = tasksByKey .get (key );
@@ -110,11 +92,16 @@ protected void invokeCallback(final Deque<Runnable> tasks) {
11092 }
11193 }
11294
113- private static class AutoExecutor extends SequentialExecutor {
95+ static class AutoExecutor extends SequentialExecutor {
11496 AutoExecutor (Executor executor ) {
11597 super (executor );
11698 }
11799
100+ /** Runs synchronous {@code Runnable} tasks sequentially. */
101+ void submit (String key , Runnable task ) {
102+ super .execute (key , task );
103+ }
104+
118105 @ Override
119106 protected void execute (final String key , final Deque <Runnable > tasks ) {
120107 executor .execute (
@@ -142,12 +129,21 @@ private void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable
142129 }
143130 }
144131
145- private static class CallbackExecutor extends SequentialExecutor {
132+ /**
133+ * Runs asynchronous {@code Callable} tasks sequentially for the same key. If one of the tasks fails, other tasks
134+ * with the same key that have not been executed will be cancelled.
135+ */
136+ static class CallbackExecutor extends SequentialExecutor {
137+ private static final Logger logger = Logger .getLogger (SequentialExecutorService .SequentialExecutor .class .getName ());
138+
146139 CallbackExecutor (Executor executor ) {
147140 super (executor );
148141 }
149142
150143 /**
144+ * Runs asynchronous {@code Callable} tasks sequentially. If one of the tasks fails, other tasks
145+ * with the same key that have not been executed will be cancelled.
146+ * <p>
151147 * This method does the following in a chain:
152148 *
153149 * <ol>
0 commit comments