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