Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
rom1v committed Apr 22, 2013
commit dbd7cafdefb215c02b3242203c8f4481855bbbb1
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.util.Log;

public class BackgroundExecutor {

private static final String TAG = "BackgroundExecutor";

private static Executor executor = Executors.newScheduledThreadPool(2 * Runtime.getRuntime().availableProcessors());

private static final List<Task> tasks = new ArrayList<Task>();
Expand Down Expand Up @@ -82,9 +86,6 @@ public static synchronized void execute(Task task) {
if (task.serial == null || !hasSerialRunning(task.serial)) {
task.executionAsked = true;
future = directExecute(task, task.delay);
if (task.id != null && future == null) {
throw new IllegalArgumentException("The executor set does not support task cancellation");
}
}
if (task.id != null || task.serial != null) {
/* keep task */
Expand Down Expand Up @@ -196,6 +197,8 @@ public static synchronized void cancelAll(String id, boolean mayInterruptIfRunni
tasks.remove(i);
if (task.future != null) {
task.future.cancel(mayInterruptIfRunning);
} else if (task.executionAsked) {
Log.w(TAG, "A task with id " + task.id + " cannot be cancelled (the executor set does not support it)");
}
}
}
Expand Down