Simplified executors

pull/212/head
M66B 2 years ago
parent 3e6ed3cb04
commit 51f3f150d7

@ -51,7 +51,7 @@ public class ActivityMain extends ActivityBase implements FragmentManager.OnBack
private static final long IGNORE_STORAGE_SPACE = 24 * 60 * 60 * 1000L; // milliseconds private static final long IGNORE_STORAGE_SPACE = 24 * 60 * 60 * 1000L; // milliseconds
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(0, 1, 3, "main"); Helper.getBackgroundExecutor(1, "main");
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {

@ -337,9 +337,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private DateFormat DTF; private DateFormat DTF;
private static final ExecutorService executorDiffer = private static final ExecutorService executorDiffer =
Helper.getBackgroundExecutor(0, 0, 3, "differ"); Helper.getBackgroundExecutor(0, "differ");
private static final ExecutorService executorAvatar = private static final ExecutorService executorAvatar =
Helper.getBackgroundExecutor(0, 0, 3, "avatar"); Helper.getBackgroundExecutor(0, "avatar");
private static final int MAX_RECIPIENTS_COMPACT = 3; private static final int MAX_RECIPIENTS_COMPACT = 3;
private static final int MAX_RECIPIENTS_NORMAL = 7; private static final int MAX_RECIPIENTS_NORMAL = 7;

@ -127,7 +127,7 @@ public abstract class DB extends RoomDatabase {
private static final int DB_CHECKPOINT = 1000; // requery/sqlite-android default private static final int DB_CHECKPOINT = 1000; // requery/sqlite-android default
private static ExecutorService executor = private static ExecutorService executor =
Helper.getBackgroundExecutor(1, 0, 3, "db"); Helper.getBackgroundExecutor(0, "db");
private static final String[] DB_TABLES = new String[]{ private static final String[] DB_TABLES = new String[]{
"identity", "account", "folder", "message", "attachment", "operation", "contact", "certificate", "answer", "rule", "search", "log"}; "identity", "account", "folder", "message", "attachment", "operation", "contact", "certificate", "answer", "rule", "search", "log"};

@ -268,19 +268,19 @@ public class Helper {
static ExecutorService getUIExecutor() { static ExecutorService getUIExecutor() {
if (sUIExecutor == null) if (sUIExecutor == null)
sUIExecutor = getBackgroundExecutor(0, 0, 3, "UI"); sUIExecutor = getBackgroundExecutor(0, "UI");
return sUIExecutor; return sUIExecutor;
} }
static ExecutorService getMediaTaskExecutor() { static ExecutorService getMediaTaskExecutor() {
if (sMediaExecutor == null) if (sMediaExecutor == null)
sMediaExecutor = getBackgroundExecutor(0, 1, 3, "media"); sMediaExecutor = getBackgroundExecutor(1, "media");
return sMediaExecutor; return sMediaExecutor;
} }
static ExecutorService getDownloadTaskExecutor() { static ExecutorService getDownloadTaskExecutor() {
if (sDownloadExecutor == null) if (sDownloadExecutor == null)
sDownloadExecutor = getBackgroundExecutor(0, 0, 3, "download"); sDownloadExecutor = getBackgroundExecutor(0, "download");
return sDownloadExecutor; return sDownloadExecutor;
} }
@ -295,10 +295,6 @@ public class Helper {
} }
static ExecutorService getBackgroundExecutor(int threads, final String name) { static ExecutorService getBackgroundExecutor(int threads, final String name) {
return getBackgroundExecutor(threads == 0 ? -1 : threads, threads, 3, name);
}
static ExecutorService getBackgroundExecutor(int min, int max, int keepalive, final String name) {
ThreadFactory factory = new ThreadFactory() { ThreadFactory factory = new ThreadFactory() {
private final AtomicInteger threadId = new AtomicInteger(); private final AtomicInteger threadId = new AtomicInteger();
@ -324,22 +320,23 @@ public class Helper {
} }
}; };
if (max == 0) { if (threads == 0) {
// java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again // java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
// 1040 KB native stack size / 32 KB thread stack size ~ 32 threads // 1040 KB native stack size / 32 KB thread stack size ~ 32 threads
int processors = Runtime.getRuntime().availableProcessors(); // Modern devices: 8 int processors = Runtime.getRuntime().availableProcessors(); // Modern devices: 8
threads = Math.max(8, processors * 2) + 1;
return new ThreadPoolExecutorEx( return new ThreadPoolExecutorEx(
name, name,
min < 0 ? Math.max(2, processors / 2 + 1) : min, threads,
Math.max(8, processors * 2) + 1, threads,
keepalive, TimeUnit.SECONDS, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), new LinkedBlockingQueue<Runnable>(),
factory); factory);
} else if (max == 1) } else if (threads == 1)
return new ThreadPoolExecutorEx( return new ThreadPoolExecutorEx(
name, name,
min, max, threads, threads,
keepalive, TimeUnit.SECONDS, 3, TimeUnit.SECONDS,
new PriorityBlockingQueue<Runnable>(10, new PriorityComparator()), new PriorityBlockingQueue<Runnable>(10, new PriorityComparator()),
factory) { factory) {
private final AtomicLong sequenceId = new AtomicLong(); private final AtomicLong sequenceId = new AtomicLong();
@ -358,8 +355,8 @@ public class Helper {
else else
return new ThreadPoolExecutorEx( return new ThreadPoolExecutorEx(
name, name,
min, max, threads, threads,
keepalive, TimeUnit.SECONDS, 3, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), new LinkedBlockingQueue<Runnable>(),
factory); factory);
} }
@ -374,6 +371,8 @@ public class Helper {
BlockingQueue<Runnable> workQueue, BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) { ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
if (keepAliveTime != 0)
allowCoreThreadTimeOut(true);
this.name = name; this.name = name;
} }

@ -85,7 +85,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
private List<Long> handling = new ArrayList<>(); private List<Long> handling = new ArrayList<>();
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(0, 1, 3, "send"); Helper.getBackgroundExecutor(1, "send");
private static final int RETRY_MAX = 3; private static final int RETRY_MAX = 3;
private static final int CONNECTIVITY_DELAY = 5000; // milliseconds private static final int CONNECTIVITY_DELAY = 5000; // milliseconds

@ -129,9 +129,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private final MediatorState liveAccountNetworkState = new MediatorState(); private final MediatorState liveAccountNetworkState = new MediatorState();
private static final ExecutorService executorService = private static final ExecutorService executorService =
Helper.getBackgroundExecutor(1, 1, 0, "sync"); Helper.getBackgroundExecutor(1, "sync");
private static final ExecutorService executorNotify = private static final ExecutorService executorNotify =
Helper.getBackgroundExecutor(0, 1, 3, "notify"); Helper.getBackgroundExecutor(1, "notify");
private static final long BACKUP_DELAY = 30 * 1000L; // milliseconds private static final long BACKUP_DELAY = 30 * 1000L; // milliseconds
private static final long PURGE_DELAY = 30 * 1000L; // milliseconds private static final long PURGE_DELAY = 30 * 1000L; // milliseconds

@ -45,7 +45,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor; 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 // This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
@ -74,10 +73,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private static final List<SimpleTask> tasks = new ArrayList<>(); private static final List<SimpleTask> tasks = new ArrayList<>();
private static final ExecutorService serialExecutor = private static final ExecutorService serialExecutor =
Helper.getBackgroundExecutor(0, 1, 3, "tasks/serial"); Helper.getBackgroundExecutor(1, "tasks/serial");
private static final ExecutorService globalExecutor = private static final ExecutorService globalExecutor =
Helper.getBackgroundExecutor(0, 0, 3, "tasks/global"); Helper.getBackgroundExecutor(0, "tasks/global");
private static final int REPORT_AFTER = 15 * 60 * 1000; // milliseconds private static final int REPORT_AFTER = 15 * 60 * 1000; // milliseconds

@ -68,7 +68,7 @@ public class TextHelper {
private static final long MAX_CONVERSATION_DURATION = 3000; // milliseconds private static final long MAX_CONVERSATION_DURATION = 3000; // milliseconds
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(0, 1, 3, "text"); Helper.getBackgroundExecutor(1, "text");
static { static {
System.loadLibrary("fairemail"); System.loadLibrary("fairemail");

@ -69,7 +69,7 @@ public class ViewModelMessages extends ViewModel {
}; };
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(0, 0, 3, "model"); Helper.getBackgroundExecutor(0, "model");
private static final int LOCAL_PAGE_SIZE = 50; private static final int LOCAL_PAGE_SIZE = 50;
private static final int THREAD_PAGE_SIZE = 100; private static final int THREAD_PAGE_SIZE = 100;

Loading…
Cancel
Save