1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-24 16:53:37 +00:00

Allow setting simple task executor

This commit is contained in:
M66B 2020-10-19 14:21:48 +02:00
parent a12f3068cb
commit bcb9f8963d

View file

@ -54,8 +54,9 @@ public abstract class SimpleTask<T> 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<SimpleTask> tasks = new ArrayList<>();
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
@ -70,6 +71,25 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
return this;
}
public SimpleTask<T> 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<T> 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;