diff --git a/app/src/main/java/eu/faircode/email/SimpleTask.java b/app/src/main/java/eu/faircode/email/SimpleTask.java index 727f04fd46..150a3761fd 100644 --- a/app/src/main/java/eu/faircode/email/SimpleTask.java +++ b/app/src/main/java/eu/faircode/email/SimpleTask.java @@ -54,8 +54,9 @@ public abstract class SimpleTask implements LifecycleObserver { private String name; private Future future; + private ExecutorService localExecutor; - private static ExecutorService executor = null; + private static ExecutorService globalExecutor = null; private static final List tasks = new ArrayList<>(); static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT"; @@ -70,6 +71,25 @@ public abstract class SimpleTask implements LifecycleObserver { return this; } + public SimpleTask setExecutor(ExecutorService executor) { + this.localExecutor = executor; + return this; + } + + private ExecutorService getExecutor(Context context) { + if (localExecutor != null) + return localExecutor; + + if (globalExecutor == null) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + int threads = prefs.getInt("query_threads", Runtime.getRuntime().availableProcessors()); + Log.i("Task threads=" + threads); + globalExecutor = Helper.getBackgroundExecutor(threads, "task"); + } + + return globalExecutor; + } + public void execute(Context context, LifecycleOwner owner, @NonNull Bundle args, @NonNull String name) { run(context, owner, args, name); } @@ -114,14 +134,7 @@ public abstract class SimpleTask implements LifecycleObserver { onException(args, ex); } - if (executor == null) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - int threads = prefs.getInt("query_threads", Runtime.getRuntime().availableProcessors()); - Log.i("Task threads=" + threads); - executor = Helper.getBackgroundExecutor(threads, "task"); - } - - future = executor.submit(new Runnable() { + future = getExecutor(context).submit(new Runnable() { private Object data; private long elapsed; private Throwable ex;