• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.util;
17 
18 import android.os.HandlerThread;
19 import android.os.Looper;
20 import android.os.Process;
21 
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.ThreadFactory;
26 import java.util.concurrent.ThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicInteger;
29 
30 /**
31  * Various different executors used in Launcher
32  */
33 public class Executors {
34 
35     private static final int POOL_SIZE =
36             Math.max(Runtime.getRuntime().availableProcessors(), 2);
37     private static final int KEEP_ALIVE = 1;
38 
39     /** Dedicated executor instances for work depending on other packages. */
40     private static final Map<String, LooperExecutor> PACKAGE_EXECUTORS = new ConcurrentHashMap<>();
41 
42     /**
43      * An {@link ThreadPoolExecutor} to be used with async task with no limit on the queue size.
44      */
45     public static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
46             POOL_SIZE, POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
47 
48     /**
49      * Returns the executor for running tasks on the main thread.
50      */
51     public static final LooperExecutor MAIN_EXECUTOR =
52             new LooperExecutor(Looper.getMainLooper());
53 
54     /**
55      * A background executor for using time sensitive actions where user is waiting for response.
56      */
57     public static final LooperExecutor UI_HELPER_EXECUTOR =
58             new LooperExecutor(
59                     createAndStartNewLooper("UiThreadHelper", Process.THREAD_PRIORITY_FOREGROUND));
60 
61     /**
62      * Utility method to get a started handler thread statically
63      */
createAndStartNewLooper(String name)64     public static Looper createAndStartNewLooper(String name) {
65         return createAndStartNewLooper(name, Process.THREAD_PRIORITY_DEFAULT);
66     }
67 
68     /**
69      * Utility method to get a started handler thread statically with the provided priority
70      */
createAndStartNewLooper(String name, int priority)71     public static Looper createAndStartNewLooper(String name, int priority) {
72         HandlerThread thread = new HandlerThread(name, priority);
73         thread.start();
74         return thread.getLooper();
75     }
76 
77     /**
78      * Executor used for running Launcher model related tasks (eg loading icons or updated db)
79      */
80     public static final LooperExecutor MODEL_EXECUTOR =
81             new LooperExecutor(createAndStartNewLooper("launcher-loader"));
82 
83     /**
84      * Returns and caches a single thread executor for a given package.
85      *
86      * @param packageName Package associated with the executor.
87      */
getPackageExecutor(String packageName)88     public static LooperExecutor getPackageExecutor(String packageName) {
89         return PACKAGE_EXECUTORS.computeIfAbsent(
90                 packageName, p -> new LooperExecutor(
91                         createAndStartNewLooper(p, Process.THREAD_PRIORITY_DEFAULT)));
92     }
93 
94     /**
95      * A simple ThreadFactory to set the thread name and priority when used with executors.
96      */
97     public static class SimpleThreadFactory implements ThreadFactory {
98 
99         private final int mPriority;
100         private final String mNamePrefix;
101 
102         private final AtomicInteger mCount = new AtomicInteger(0);
103 
SimpleThreadFactory(String namePrefix, int priority)104         public SimpleThreadFactory(String namePrefix, int priority) {
105             mNamePrefix = namePrefix;
106             mPriority = priority;
107         }
108 
109         @Override
newThread(Runnable runnable)110         public Thread newThread(Runnable runnable) {
111             Thread t = new Thread(() -> {
112                 Process.setThreadPriority(mPriority);
113                 runnable.run();
114             }, mNamePrefix + mCount.incrementAndGet());
115             return t;
116         }
117     }
118 }
119