diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/BackgroundExecutor.java b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/BackgroundExecutor.java index f3296428bc..1b1b25b787 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/BackgroundExecutor.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/BackgroundExecutor.java @@ -128,16 +128,14 @@ private static Future directExecute(Runnable runnable, long delay) { * executor) */ public static synchronized void execute(Task task) { - Future future = null; - if (task.serial == null || !hasSerialRunning(task.serial)) { - task.executionAsked = true; - future = directExecute(task, task.remainingDelay); - } - if ((task.id != null || task.serial != null) && !task.managed.get()) { + if (task.id != null || task.serial != null) { /* keep task */ - task.future = future; TASKS.add(task); } + if (task.serial == null || !hasSerialRunning(task.serial)) { + task.executionAsked = true; + task.future = directExecute(task, task.remainingDelay); + } } /** diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java index c86d16e941..71487e431e 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ThreadActivityTest.java @@ -163,6 +163,31 @@ public void parallelBackgroundTasks() { } } + @Test + public void serializedBackgroundTasksAsynchronous() throws InterruptedException { + /* set an executor with 4 threads */ + BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4)); + + testSerializedBackgroundTasks(); + } + + @Test + public void serializedBackgroundTasksSynchronous() throws InterruptedException { + /* set a synchronous executor */ + BackgroundExecutor.setExecutor(new Executor() { + @Override + public void execute(Runnable command) { + try { + command.run(); + } catch (RuntimeException ignored) { + // ignored + } + } + }); + + testSerializedBackgroundTasks(); + } + /** * Verify that serialized background tasks are correctly serialized. * @@ -173,14 +198,10 @@ public void parallelBackgroundTasks() { * Once all tasks have completed execution, verify that the items in the * list are ordered. */ - @Test - public void serializedBackgroundTasks() { + private void testSerializedBackgroundTasks() { /* number of items to add to the list */ final int NB_ADD = 10; - /* set an executor with 4 threads */ - BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4)); - /* * the calls are serialized, but not necessarily on the same thread, so * we need to synchronize to avoid cache effects