Merge "Add support for dedicated executors for package tasks." into tm-dev

This commit is contained in:
Brian Isganitis
2022-03-10 00:29:37 +00:00
committed by Android (Google) Code Review
@@ -15,10 +15,17 @@
*/ */
package com.android.launcher3.util; package com.android.launcher3.util;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper; import android.os.Looper;
import android.os.Process; import android.os.Process;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@@ -34,6 +41,9 @@ public class Executors {
Math.max(Runtime.getRuntime().availableProcessors(), 2); Math.max(Runtime.getRuntime().availableProcessors(), 2);
private static final int KEEP_ALIVE = 1; private static final int KEEP_ALIVE = 1;
/** Dedicated executor instances for work depending on other packages. */
private static final Map<String, ExecutorService> PACKAGE_EXECUTORS = new ConcurrentHashMap<>();
/** /**
* An {@link ThreadPoolExecutor} to be used with async task with no limit on the queue size. * An {@link ThreadPoolExecutor} to be used with async task with no limit on the queue size.
*/ */
@@ -75,6 +85,18 @@ public class Executors {
public static final LooperExecutor MODEL_EXECUTOR = public static final LooperExecutor MODEL_EXECUTOR =
new LooperExecutor(createAndStartNewLooper("launcher-loader")); new LooperExecutor(createAndStartNewLooper("launcher-loader"));
/**
* Returns and caches a single thread executor for a given package.
*
* @param packageName Package associated with the executor.
*/
public static ExecutorService getPackageExecutor(String packageName) {
return PACKAGE_EXECUTORS.computeIfAbsent(
packageName,
p -> newSingleThreadExecutor(
new SimpleThreadFactory(p, THREAD_PRIORITY_BACKGROUND)));
}
/** /**
* A simple ThreadFactory to set the thread name and priority when used with executors. * A simple ThreadFactory to set the thread name and priority when used with executors.
*/ */