Dequeue cancelled tasks

This commit is contained in:
M66B 2023-01-13 14:55:24 +01:00
parent 1cf9365c76
commit 2542018ca9
1 changed files with 7 additions and 0 deletions

View File

@ -45,6 +45,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor;
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
// Make sure to not access any member in any outer scope from onExecute
@ -358,6 +360,11 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
}
void cancel(Context context) {
ExecutorService executor = getExecutor(context);
if (executor instanceof ThreadPoolExecutor && future instanceof Runnable) {
boolean removed = ((ThreadPoolExecutor) executor).remove((Runnable) future);
Log.i("Remove task=" + name + " removed=" + removed);
}
if (future != null && future.cancel(false)) {
Log.i("Cancelled task=" + name);
cleanup(context);