Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 9985263

Browse files
committed
Support synchronous executors
BackgroundExecutor assumed that its executor was asynchronous, so that in execute(Task), a task could not be removed before it has been added to the TASKS list. However, this assumption was wrong with a synchronous executor: directExecute(…) would execute the task synchronously, leading to call Task#postExecute(), that removes the task from TASKS. Immediately after the execution, directExecute(…) would add the task to TASKS, which let the BackgroundExecutor in an incorrect state. Therefore, add the task to the TASKS list before the execution, so that it will also work with synchronous executors. Fixes #1689. <#1689>
1 parent a003c49 commit 9985263

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/BackgroundExecutor.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,14 @@ private static Future<?> directExecute(Runnable runnable, long delay) {
128128
* executor)
129129
*/
130130
public static synchronized void execute(Task task) {
131-
Future<?> future = null;
132-
if (task.serial == null || !hasSerialRunning(task.serial)) {
133-
task.executionAsked = true;
134-
future = directExecute(task, task.remainingDelay);
135-
}
136131
if (task.id != null || task.serial != null) {
137132
/* keep task */
138-
task.future = future;
139133
TASKS.add(task);
140134
}
135+
if (task.serial == null || !hasSerialRunning(task.serial)) {
136+
task.executionAsked = true;
137+
task.future = directExecute(task, task.remainingDelay);
138+
}
141139
}
142140

143141
/**

‎AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ public void parallelBackgroundTasks() {
163163
}
164164
}
165165

166+
@Test
167+
public void serializedBackgroundTasksAsynchronous() throws InterruptedException {
168+
/* set an executor with 4 threads */
169+
BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4));
170+
171+
testSerializedBackgroundTasks();
172+
}
173+
174+
@Test
175+
public void serializedBackgroundTasksSynchronous() throws InterruptedException {
176+
/* set a synchronous executor */
177+
BackgroundExecutor.setExecutor(new Executor() {
178+
@Override
179+
public void execute(Runnable command) {
180+
try {
181+
command.run();
182+
} catch (RuntimeException ignored) {
183+
// ignored
184+
}
185+
}
186+
});
187+
188+
testSerializedBackgroundTasks();
189+
}
190+
166191
/**
167192
* Verify that serialized background tasks are correctly serialized.
168193
*
@@ -173,14 +198,10 @@ public void parallelBackgroundTasks() {
173198
* Once all tasks have completed execution, verify that the items in the
174199
* list are ordered.
175200
*/
176-
@Test
177-
public void serializedBackgroundTasks() {
201+
private void testSerializedBackgroundTasks() {
178202
/* number of items to add to the list */
179203
final int NB_ADD = 10;
180204

181-
/* set an executor with 4 threads */
182-
BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4));
183-
184205
/*
185206
* the calls are serialized, but not necessarily on the same thread, so
186207
* we need to synchronize to avoid cache effects

0 commit comments

Comments
 (0)