mirror of https://github.com/M66B/FairEmail.git
Allow setting simple task executor
This commit is contained in:
parent
a12f3068cb
commit
bcb9f8963d
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue