Simplified executors

This commit is contained in:
M66B 2023-01-13 17:38:41 +01:00
parent 3e6ed3cb04
commit 51f3f150d7
9 changed files with 26 additions and 28 deletions

View File

@ -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 ExecutorService executor =
Helper.getBackgroundExecutor(0, 1, 3, "main");
Helper.getBackgroundExecutor(1, "main");
@Override
protected void onCreate(Bundle savedInstanceState) {

View File

@ -337,9 +337,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private DateFormat DTF;
private static final ExecutorService executorDiffer =
Helper.getBackgroundExecutor(0, 0, 3, "differ");
Helper.getBackgroundExecutor(0, "differ");
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_NORMAL = 7;

View File

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

View File

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

View File

@ -85,7 +85,7 @@ public class ServiceSend extends ServiceBase implements SharedPreferences.OnShar
private List<Long> handling = new ArrayList<>();
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 CONNECTIVITY_DELAY = 5000; // milliseconds

View File

@ -129,9 +129,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private final MediatorState liveAccountNetworkState = new MediatorState();
private static final ExecutorService executorService =
Helper.getBackgroundExecutor(1, 1, 0, "sync");
Helper.getBackgroundExecutor(1, "sync");
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 PURGE_DELAY = 30 * 1000L; // milliseconds

View File

@ -45,7 +45,6 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
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
@ -74,10 +73,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private static final List<SimpleTask> tasks = new ArrayList<>();
private static final ExecutorService serialExecutor =
Helper.getBackgroundExecutor(0, 1, 3, "tasks/serial");
Helper.getBackgroundExecutor(1, "tasks/serial");
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

View File

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

View File

@ -69,7 +69,7 @@ public class ViewModelMessages extends ViewModel {
};
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 THREAD_PAGE_SIZE = 100;