Skip to content

Commit dbd7caf

Browse files
committed
Do not fail if cancellation is not supported
If the executor is replaced by another not supporting cancellation (not extending ExecutorService), it failed the first time a task with a non-null id was requested. Now, it just warns during cancellation.
1 parent 0ec12b0 commit dbd7caf

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
import java.util.concurrent.ScheduledExecutorService;
2525
import java.util.concurrent.TimeUnit;
2626

27+
import android.util.Log;
28+
2729
public class BackgroundExecutor {
2830

31+
private static final String TAG = "BackgroundExecutor";
32+
2933
private static Executor executor = Executors.newScheduledThreadPool(2 * Runtime.getRuntime().availableProcessors());
3034

3135
private static final List<Task> tasks = new ArrayList<Task>();
@@ -82,9 +86,6 @@ public static synchronized void execute(Task task) {
8286
if (task.serial == null || !hasSerialRunning(task.serial)) {
8387
task.executionAsked = true;
8488
future = directExecute(task, task.delay);
85-
if (task.id != null && future == null) {
86-
throw new IllegalArgumentException("The executor set does not support task cancellation");
87-
}
8889
}
8990
if (task.id != null || task.serial != null) {
9091
/* keep task */
@@ -196,6 +197,8 @@ public static synchronized void cancelAll(String id, boolean mayInterruptIfRunni
196197
tasks.remove(i);
197198
if (task.future != null) {
198199
task.future.cancel(mayInterruptIfRunning);
200+
} else if (task.executionAsked) {
201+
Log.w(TAG, "A task with id " + task.id + " cannot be cancelled (the executor set does not support it)");
199202
}
200203
}
201204
}

0 commit comments

Comments
 (0)