• 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.ExecutorService;
25 import java.util.concurrent.LinkedBlockingQueue;
26 import java.util.concurrent.ThreadFactory;
27 import java.util.concurrent.ThreadPoolExecutor;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.atomic.AtomicInteger;
30 
31 /**
32  * Various different executors used in Launcher
33  */
34 public class Executors {
35 
36     private static final int POOL_SIZE =
37             Math.max(Runtime.getRuntime().availableProcessors(), 2);
38     private static final int KEEP_ALIVE = 1;
39 
40     /** Dedicated executor instances for work depending on other packages. */
41     private static final Map<String, LooperExecutor> PACKAGE_EXECUTORS = new ConcurrentHashMap<>();
42 
43     /**
44      * An {@link ThreadPoolExecutor} to be used with async task with no limit on the queue size.
45      */
46     public static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
47             POOL_SIZE, POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
48 
49     /**
50      * Returns the executor for running tasks on the main thread.
51      */
52     public static final LooperExecutor MAIN_EXECUTOR =
53             new LooperExecutor(Looper.getMainLooper());
54 
55     /**
56      * A background executor for using time sensitive actions where user is waiting for response.
57      */
58     public static final LooperExecutor UI_HELPER_EXECUTOR =
59             new LooperExecutor(
60                     createAndStartNewLooper("UiThreadHelper", Process.THREAD_PRIORITY_FOREGROUND));
61 
62 
63     /** A background executor to preinflate views. */
64     public static final ExecutorService VIEW_PREINFLATION_EXECUTOR =
65             java.util.concurrent.Executors.newSingleThreadExecutor();
66 
67     /**
68      * Utility method to get a started handler thread statically
69      */
createAndStartNewLooper(String name)70     public static Looper createAndStartNewLooper(String name) {
71         return createAndStartNewLooper(name, Process.THREAD_PRIORITY_DEFAULT);
72     }
73 
74     /**
75      * Utility method to get a started handler thread statically with the provided priority
76      */
createAndStartNewLooper(String name, int priority)77     public static Looper createAndStartNewLooper(String name, int priority) {
78         HandlerThread thread = new HandlerThread(name, priority);
79         thread.start();
80         return thread.getLooper();
81     }
82 
83     /**
84      * Executor used for running Launcher model related tasks (eg loading icons or updated db)
85      */
86     public static final LooperExecutor MODEL_EXECUTOR =
87             new LooperExecutor(createAndStartNewLooper("launcher-loader"));
88 
89     /**
90      * Returns and caches a single thread executor for a given package.
91      *
92      * @param packageName Package associated with the executor.
93      */
getPackageExecutor(String packageName)94     public static LooperExecutor getPackageExecutor(String packageName) {
95         return PACKAGE_EXECUTORS.computeIfAbsent(
96                 packageName, p -> new LooperExecutor(
97                         createAndStartNewLooper(p, Process.THREAD_PRIORITY_DEFAULT)));
98     }
99 
100     /**
101      * A simple ThreadFactory to set the thread name and priority when used with executors.
102      */
103     public static class SimpleThreadFactory implements ThreadFactory {
104 
105         private final int mPriority;
106         private final String mNamePrefix;
107 
108         private final AtomicInteger mCount = new AtomicInteger(0);
109 
SimpleThreadFactory(String namePrefix, int priority)110         public SimpleThreadFactory(String namePrefix, int priority) {
111             mNamePrefix = namePrefix;
112             mPriority = priority;
113         }
114 
115         @Override
newThread(Runnable runnable)116         public Thread newThread(Runnable runnable) {
117             Thread t = new Thread(() -> {
118                 Process.setThreadPriority(mPriority);
119                 runnable.run();
120             }, mNamePrefix + mCount.incrementAndGet());
121             return t;
122         }
123     }
124 }
125