Skip to content

Commit

Permalink
Refactoring SequentialExecutorService.java (#4979)
Browse files Browse the repository at this point in the history
- Marking methods as private
- renaming variables from `finalTasks` to `tasks` for clarity
- reducing code duplication by calling `execute`
  • Loading branch information
sduskis committed Apr 18, 2019
1 parent 7487e88 commit 708d785
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ private static class AutoExecutor extends SequentialExecutor {
super(executor);
}

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

protected void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable> tasks) {
private void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable> tasks) {
invokeCallback(tasks);
synchronized (tasksByKey) {
if (tasks.isEmpty()) {
Expand All @@ -137,13 +137,7 @@ protected void invokeCallbackAndExecuteNext(final String key, final Deque<Runnab
return;
}
}
executor.execute(
new Runnable() {
@Override
public void run() {
invokeCallbackAndExecuteNext(key, tasks);
}
});
execute(key, tasks);
}
}

Expand Down Expand Up @@ -199,18 +193,18 @@ public void cancel(Throwable e) {
return future;
}

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

/** Executes the next queued task associated with {@code key}. */
void resume(final String key) {
private void resume(String key) {
Deque<Runnable> tasks;
synchronized (tasksByKey) {
tasks = tasksByKey.get(key);
Expand All @@ -222,19 +216,11 @@ void resume(final String key) {
return;
}
}
final Deque<Runnable> finalTasks = tasks;
// Run the next task.
executor.execute(
new Runnable() {
@Override
public void run() {
invokeCallback(finalTasks);
}
});
execute(key, tasks);
}

/** Cancels every task in the queue assoicated with {@code key}. */
void cancelQueuedTasks(final String key, Throwable e) {
private 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) {
Expand Down

0 comments on commit 708d785

Please sign in to comment.