Made simple task multi threading

pull/91/head
M66B 7 years ago
parent e64a43ff64
commit b5d71b783c

@ -22,9 +22,13 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
@ -47,15 +51,14 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private Bundle args = null; private Bundle args = null;
private Result stored = null; private Result stored = null;
private static HandlerThread handlerThread; private ExecutorService executor = Executors.newFixedThreadPool(10, new ThreadFactory() {
private static Handler handler; @Override
public Thread newThread(@NonNull Runnable runnable) {
static { Thread thread = new Thread(runnable);
handlerThread = new HandlerThread("SimpleTask"); thread.setPriority(THREAD_PRIORITY_BACKGROUND);
handlerThread.start(); return thread;
handlerThread.setPriority(THREAD_PRIORITY_BACKGROUND); }
handler = new Handler(handlerThread.getLooper()); });
}
public void load(Context context, LifecycleOwner owner, Bundle args) { public void load(Context context, LifecycleOwner owner, Bundle args) {
run(context, owner, args); run(context, owner, args);
@ -120,7 +123,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
owner.getLifecycle().addObserver(this); owner.getLifecycle().addObserver(this);
// Run in background thread // Run in background thread
handler.post(new Runnable() { executor.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
final Result result = new Result(); final Result result = new Result();

Loading…
Cancel
Save