Skip to content

Commit

Permalink
Moving methods from SequentialExecutor to subclasses
Browse files Browse the repository at this point in the history
The changes were literally cut and paste.
  • Loading branch information
sduskis committed Apr 18, 2019
1 parent b68add0 commit 7cb3398
Showing 1 changed file with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,27 @@ void execute(final String key, Runnable task) {

protected abstract void execute(String key, Deque<Runnable> finalTasks);

/** Cancels every task in the queue assoicated with {@code key}. */
void cancelQueuedTasks(final String key, Throwable e) {
// TODO(kimkyung-goog): Ensure execute() fails once cancelQueueTasks() has been ever invoked,
// so that no more tasks are scheduled.
synchronized (tasksByKey) {
final Deque<Runnable> tasks = tasksByKey.get(key);
if (tasks == null) {
return;
}
while (!tasks.isEmpty()) {
Runnable task = tasks.poll();
if (task instanceof CancellableRunnable) {
((CancellableRunnable) task).cancel(e);
} else {
logger.log(
Level.WARNING,
"Attempted to cancel Runnable that was not CancellableRunnable; ignored.");
}
}
}
}

protected void invokeCallback(final Deque<Runnable> tasks) {
// TODO(kimkyung-goog): Check if there is a race when task list becomes empty.
Runnable task = tasks.poll();
if (task != null) {
task.run();
}
}
}

private static class AutoExecutor extends SequentialExecutor {
AutoExecutor(Executor executor) {
super(executor);
}

protected void execute(final String key, final Deque<Runnable> finalTasks) {
executor.execute(new Runnable() {
@Override public void run() {
invokeCallbackAndExecuteNext(key, finalTasks);
}
});
}

protected void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable> tasks) {
invokeCallback(tasks);
Expand All @@ -153,20 +145,6 @@ public void run() {
}
}

private static class AutoExecutor extends SequentialExecutor {
AutoExecutor(Executor executor) {
super(executor);
}

protected void execute(final String key, final Deque<Runnable> finalTasks) {
executor.execute(new Runnable() {
@Override public void run() {
invokeCallbackAndExecuteNext(key, finalTasks);
}
});
}
}

private static class CallbackExecutor extends SequentialExecutor {
CallbackExecutor(Executor executor) {
super(executor);
Expand Down Expand Up @@ -247,5 +225,27 @@ public void run() {
}
});
}

/** Cancels every task in the queue assoicated with {@code key}. */
void cancelQueuedTasks(final String key, Throwable e) {
// TODO(kimkyung-goog): Ensure execute() fails once cancelQueueTasks() has been ever invoked,
// so that no more tasks are scheduled.
synchronized (tasksByKey) {
final Deque<Runnable> tasks = tasksByKey.get(key);
if (tasks == null) {
return;
}
while (!tasks.isEmpty()) {
Runnable task = tasks.poll();
if (task instanceof CancellableRunnable) {
((CancellableRunnable) task).cancel(e);
} else {
logger.log(
Level.WARNING,
"Attempted to cancel Runnable that was not CancellableRunnable; ignored.");
}
}
}
}
}
}

0 comments on commit 7cb3398

Please sign in to comment.