|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | +package bolts; |
| 8 | + |
| 9 | +import android.annotation.SuppressLint; |
| 10 | +import android.os.Build; |
| 11 | +import android.os.Handler; |
| 12 | +import android.os.Looper; |
| 13 | + |
| 14 | +import java.util.concurrent.Executor; |
| 15 | +import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.LinkedBlockingQueue; |
| 17 | +import java.util.concurrent.ThreadFactory; |
| 18 | +import java.util.concurrent.ThreadPoolExecutor; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +/** |
| 22 | + * This was created because the helper methods in {@link java.util.concurrent.Executors} do not work |
| 23 | + * as people would normally expect. |
| 24 | + * <p> |
| 25 | + * Normally, you would think that a cached thread pool would create new threads when necessary, |
| 26 | + * queue them when the pool is full, and kill threads when they've been inactive for a certain |
| 27 | + * period of time. This is not how {@link java.util.concurrent.Executors#newCachedThreadPool()} |
| 28 | + * works. |
| 29 | + * <p> |
| 30 | + * Instead, {@link java.util.concurrent.Executors#newCachedThreadPool()} executes all tasks on |
| 31 | + * a new or cached thread immediately because corePoolSize is 0, SynchronousQueue is a queue with |
| 32 | + * size 0 and maxPoolSize is Integer.MAX_VALUE. This is dangerous because it can create an unchecked |
| 33 | + * amount of threads. |
| 34 | + */ |
| 35 | +/* package */ final class AndroidExecutors { |
| 36 | + |
| 37 | + private static final AndroidExecutors INSTANCE = new AndroidExecutors(); |
| 38 | + |
| 39 | + private final Executor uiThread; |
| 40 | + |
| 41 | + private AndroidExecutors() { |
| 42 | + uiThread = new UIThreadExecutor(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Nexus 5: Quad-Core |
| 47 | + * Moto X: Dual-Core |
| 48 | + * <p> |
| 49 | + * AsyncTask: |
| 50 | + * CORE_POOL_SIZE = CPU_COUNT + 1 |
| 51 | + * MAX_POOL_SIZE = CPU_COUNT * 2 + 1 |
| 52 | + * <p> |
| 53 | + * https://github.com/android/platform_frameworks_base/commit/719c44e03b97e850a46136ba336d729f5fbd1f47 |
| 54 | + */ |
| 55 | + private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); |
| 56 | + /* package */ static final int CORE_POOL_SIZE = CPU_COUNT + 1; |
| 57 | + /* package */ static final int MAX_POOL_SIZE = CPU_COUNT * 2 + 1; |
| 58 | + /* package */ static final long KEEP_ALIVE_TIME = 1L; |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available |
| 62 | + * or create new threads until the core pool is full. tasks will then be queued. If an |
| 63 | + * task cannot be queued, a new thread will be created unless this would exceed max pool |
| 64 | + * size, then the task will be rejected. Threads will time out after 1 second. |
| 65 | + * <p> |
| 66 | + * Core thread timeout is only available on android-9+. |
| 67 | + * |
| 68 | + * @return the newly created thread pool |
| 69 | + */ |
| 70 | + public static ExecutorService newCachedThreadPool() { |
| 71 | + ThreadPoolExecutor executor = new ThreadPoolExecutor( |
| 72 | + CORE_POOL_SIZE, |
| 73 | + MAX_POOL_SIZE, |
| 74 | + KEEP_ALIVE_TIME, TimeUnit.SECONDS, |
| 75 | + new LinkedBlockingQueue<Runnable>()); |
| 76 | + |
| 77 | + allowCoreThreadTimeout(executor, true); |
| 78 | + |
| 79 | + return executor; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available |
| 84 | + * or create new threads until the core pool is full. tasks will then be queued. If an |
| 85 | + * task cannot be queued, a new thread will be created unless this would exceed max pool |
| 86 | + * size, then the task will be rejected. Threads will time out after 1 second. |
| 87 | + * <p> |
| 88 | + * Core thread timeout is only available on android-9+. |
| 89 | + * |
| 90 | + * @param threadFactory the factory to use when creating new threads |
| 91 | + * @return the newly created thread pool |
| 92 | + */ |
| 93 | + public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
| 94 | + ThreadPoolExecutor executor = new ThreadPoolExecutor( |
| 95 | + CORE_POOL_SIZE, |
| 96 | + MAX_POOL_SIZE, |
| 97 | + KEEP_ALIVE_TIME, TimeUnit.SECONDS, |
| 98 | + new LinkedBlockingQueue<Runnable>(), |
| 99 | + threadFactory); |
| 100 | + |
| 101 | + allowCoreThreadTimeout(executor, true); |
| 102 | + |
| 103 | + return executor; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Compatibility helper function for |
| 108 | + * {@link java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)} |
| 109 | + * <p> |
| 110 | + * Only available on android-9+. |
| 111 | + * |
| 112 | + * @param executor the {@link java.util.concurrent.ThreadPoolExecutor} |
| 113 | + * @param value true if should time out, else false |
| 114 | + */ |
| 115 | + @SuppressLint("NewApi") |
| 116 | + public static void allowCoreThreadTimeout(ThreadPoolExecutor executor, boolean value) { |
| 117 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { |
| 118 | + executor.allowCoreThreadTimeOut(value); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * An {@link java.util.concurrent.Executor} that executes tasks on the UI thread. |
| 124 | + */ |
| 125 | + public static Executor uiThread() { |
| 126 | + return INSTANCE.uiThread; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * An {@link java.util.concurrent.Executor} that runs tasks on the UI thread. |
| 131 | + */ |
| 132 | + private static class UIThreadExecutor implements Executor { |
| 133 | + @Override |
| 134 | + public void execute(Runnable command) { |
| 135 | + new Handler(Looper.getMainLooper()).post(command); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments