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.Executor; 23 import java.util.concurrent.LinkedBlockingQueue; 24 import java.util.concurrent.ThreadPoolExecutor; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * Various different executors used in Launcher 29 */ 30 public class Executors { 31 32 // These values are same as that in {@link AsyncTask}. 33 private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); 34 private static final int CORE_POOL_SIZE = CPU_COUNT + 1; 35 private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; 36 private static final int KEEP_ALIVE = 1; 37 38 /** 39 * An {@link Executor} to be used with async task with no limit on the queue size. 40 */ 41 public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor( 42 CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, 43 TimeUnit.SECONDS, new LinkedBlockingQueue<>()); 44 45 /** 46 * Returns the executor for running tasks on the main thread. 47 */ 48 public static final LooperExecutor MAIN_EXECUTOR = 49 new LooperExecutor(Looper.getMainLooper()); 50 51 /** 52 * A background executor for using time sensitive actions where user is waiting for response. 53 */ 54 public static final LooperExecutor UI_HELPER_EXECUTOR = 55 new LooperExecutor(createAndStartNewForegroundLooper("UiThreadHelper")); 56 57 /** 58 * Utility method to get a started handler thread statically 59 */ createAndStartNewLooper(String name)60 public static Looper createAndStartNewLooper(String name) { 61 return createAndStartNewLooper(name, Process.THREAD_PRIORITY_DEFAULT); 62 } 63 64 /** 65 * Utility method to get a started handler thread statically with the provided priority 66 */ createAndStartNewLooper(String name, int priority)67 public static Looper createAndStartNewLooper(String name, int priority) { 68 HandlerThread thread = new HandlerThread(name, priority); 69 thread.start(); 70 return thread.getLooper(); 71 } 72 73 /** 74 * Similar to {@link #createAndStartNewLooper(String)}, but starts the thread with 75 * foreground priority. 76 * Think before using 77 */ createAndStartNewForegroundLooper(String name)78 public static Looper createAndStartNewForegroundLooper(String name) { 79 return createAndStartNewLooper(name, Process.THREAD_PRIORITY_FOREGROUND); 80 } 81 82 /** 83 * Executor used for running Launcher model related tasks (eg loading icons or updated db) 84 */ 85 public static final LooperExecutor MODEL_EXECUTOR = 86 new LooperExecutor(createAndStartNewLooper("launcher-loader")); 87 } 88