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.ThreadPoolExecutor; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * Various different executors used in Launcher 28 */ 29 public class Executors { 30 31 // These values are same as that in {@link AsyncTask}. 32 private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); 33 private static final int CORE_POOL_SIZE = CPU_COUNT + 1; 34 private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; 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 CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, 42 TimeUnit.SECONDS, new LinkedBlockingQueue<>()); 43 44 /** 45 * Returns the executor for running tasks on the main thread. 46 */ 47 public static final LooperExecutor MAIN_EXECUTOR = 48 new LooperExecutor(Looper.getMainLooper()); 49 50 /** 51 * A background executor for using time sensitive actions where user is waiting for response. 52 */ 53 public static final LooperExecutor UI_HELPER_EXECUTOR = 54 new LooperExecutor(createAndStartNewForegroundLooper("UiThreadHelper")); 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 * Similar to {@link #createAndStartNewLooper(String)}, but starts the thread with 74 * foreground priority. 75 * Think before using 76 */ createAndStartNewForegroundLooper(String name)77 public static Looper createAndStartNewForegroundLooper(String name) { 78 return createAndStartNewLooper(name, Process.THREAD_PRIORITY_FOREGROUND); 79 } 80 81 /** 82 * Executor used for running Launcher model related tasks (eg loading icons or updated db) 83 */ 84 public static final LooperExecutor MODEL_EXECUTOR = 85 new LooperExecutor(createAndStartNewLooper("launcher-loader")); 86 } 87