mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-20 08:39:22 +01:00
Improve paging performance on slower devices.
This commit is contained in:
committed by
Alex Hart
parent
8f183bdcdc
commit
62ac65e4d8
@@ -2,6 +2,10 @@ package org.thoughtcrime.securesms.util.concurrent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.android.gms.common.util.concurrent.NumberedThreadFactory;
|
||||
|
||||
import org.thoughtcrime.securesms.util.LinkedBlockingLifoQueue;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
@@ -13,7 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
public class SignalExecutors {
|
||||
|
||||
public static final ExecutorService UNBOUNDED = Executors.newCachedThreadPool(new NumberedThreadFactory("signal-unbounded"));
|
||||
public static final ExecutorService BOUNDED = Executors.newFixedThreadPool(Math.max(2, Math.min(Runtime.getRuntime().availableProcessors() - 1, 4)), new NumberedThreadFactory("signal-bounded"));
|
||||
public static final ExecutorService BOUNDED = Executors.newFixedThreadPool(getIdealThreadCount(), new NumberedThreadFactory("signal-bounded"));
|
||||
public static final ExecutorService SERIAL = Executors.newSingleThreadExecutor(new NumberedThreadFactory("signal-serial"));
|
||||
|
||||
public static ExecutorService newCachedSingleThreadExecutor(final String name) {
|
||||
@@ -22,6 +26,21 @@ public class SignalExecutors {
|
||||
return executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an executor that prioritizes newer work. This is the opposite of a traditional executor,
|
||||
* which processor work in FIFO order.
|
||||
*/
|
||||
public static ExecutorService newFixedLifoThreadExecutor(String name, int minThreads, int maxThreads) {
|
||||
return new ThreadPoolExecutor(minThreads, maxThreads, 0, TimeUnit.MILLISECONDS, new LinkedBlockingLifoQueue<>(), new NumberedThreadFactory(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an 'ideal' thread count based on the number of available processors.
|
||||
*/
|
||||
public static int getIdealThreadCount() {
|
||||
return Math.max(2, Math.min(Runtime.getRuntime().availableProcessors() - 1, 4));
|
||||
}
|
||||
|
||||
private static class NumberedThreadFactory implements ThreadFactory {
|
||||
|
||||
private final String baseName;
|
||||
|
||||
Reference in New Issue
Block a user