Skip to content

Commit 708d785

Browse files
authored
Refactoring SequentialExecutorService.java (#4979)
- Marking methods as private - renaming variables from `finalTasks` to `tasks` for clarity - reducing code duplication by calling `execute`
1 parent 7487e88 commit 708d785

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/SequentialExecutorService.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ private static class AutoExecutor extends SequentialExecutor {
115115
super(executor);
116116
}
117117

118-
protected void execute(final String key, final Deque<Runnable> finalTasks) {
118+
protected void execute(final String key, final Deque<Runnable> tasks) {
119119
executor.execute(
120120
new Runnable() {
121121
@Override
122122
public void run() {
123-
invokeCallbackAndExecuteNext(key, finalTasks);
123+
invokeCallbackAndExecuteNext(key, tasks);
124124
}
125125
});
126126
}
127127

128-
protected void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable> tasks) {
128+
private void invokeCallbackAndExecuteNext(final String key, final Deque<Runnable> tasks) {
129129
invokeCallback(tasks);
130130
synchronized (tasksByKey) {
131131
if (tasks.isEmpty()) {
@@ -137,13 +137,7 @@ protected void invokeCallbackAndExecuteNext(final String key, final Deque<Runnab
137137
return;
138138
}
139139
}
140-
executor.execute(
141-
new Runnable() {
142-
@Override
143-
public void run() {
144-
invokeCallbackAndExecuteNext(key, tasks);
145-
}
146-
});
140+
execute(key, tasks);
147141
}
148142
}
149143

@@ -199,18 +193,18 @@ public void cancel(Throwable e) {
199193
return future;
200194
}
201195

202-
protected void execute(final String key, final Deque<Runnable> finalTasks) {
196+
protected void execute(final String key, final Deque<Runnable> tasks) {
203197
executor.execute(
204198
new Runnable() {
205199
@Override
206200
public void run() {
207-
invokeCallback(finalTasks);
201+
invokeCallback(tasks);
208202
}
209203
});
210204
}
211205

212206
/** Executes the next queued task associated with {@code key}. */
213-
void resume(final String key) {
207+
private void resume(String key) {
214208
Deque<Runnable> tasks;
215209
synchronized (tasksByKey) {
216210
tasks = tasksByKey.get(key);
@@ -222,19 +216,11 @@ void resume(final String key) {
222216
return;
223217
}
224218
}
225-
final Deque<Runnable> finalTasks = tasks;
226-
// Run the next task.
227-
executor.execute(
228-
new Runnable() {
229-
@Override
230-
public void run() {
231-
invokeCallback(finalTasks);
232-
}
233-
});
219+
execute(key, tasks);
234220
}
235221

236222
/** Cancels every task in the queue assoicated with {@code key}. */
237-
void cancelQueuedTasks(final String key, Throwable e) {
223+
private void cancelQueuedTasks(final String key, Throwable e) {
238224
// TODO(kimkyung-goog): Ensure execute() fails once cancelQueueTasks() has been ever invoked,
239225
// so that no more tasks are scheduled.
240226
synchronized (tasksByKey) {

0 commit comments

Comments
 (0)