1010import android .os .Build ;
1111import android .os .Handler ;
1212import android .os .Looper ;
13-
1413import java .util .concurrent .Executor ;
1514import java .util .concurrent .ExecutorService ;
1615import java .util .concurrent .LinkedBlockingQueue ;
2120/**
2221 * This was created because the helper methods in {@link java.util.concurrent.Executors} do not work
2322 * as people would normally expect.
24- * <p>
25- * Normally, you would think that a cached thread pool would create new threads when necessary,
23+ *
24+ * <p> Normally, you would think that a cached thread pool would create new threads when necessary,
2625 * queue them when the pool is full, and kill threads when they've been inactive for a certain
2726 * period of time. This is not how {@link java.util.concurrent.Executors#newCachedThreadPool()}
2827 * 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
28+ *
29+ * <p> Instead, {@link java.util.concurrent.Executors#newCachedThreadPool()} executes all tasks on a
30+ * new or cached thread immediately because corePoolSize is 0, SynchronousQueue is a queue with size
31+ * 0 and maxPoolSize is Integer.MAX_VALUE. This is dangerous because it can create an unchecked
3332 * amount of threads.
3433 */
3534/* package */ final class AndroidExecutors {
3635
3736 /* package */ static final long KEEP_ALIVE_TIME = 1L ;
3837 private static final AndroidExecutors INSTANCE = new AndroidExecutors ();
3938 /**
40- * Nexus 5: Quad-Core
41- * Moto X: Dual-Core
42- * <p>
43- * AsyncTask:
44- * CORE_POOL_SIZE = CPU_COUNT + 1
45- * MAX_POOL_SIZE = CPU_COUNT * 2 + 1
46- * <p>
47- * https://github.com/android/platform_frameworks_base/commit/719c44e03b97e850a46136ba336d729f5fbd1f47
39+ * Nexus 5: Quad-Core Moto X: Dual-Core
40+ *
41+ * <p>AsyncTask: CORE_POOL_SIZE = CPU_COUNT + 1 MAX_POOL_SIZE = CPU_COUNT * 2 + 1
42+ *
43+ * <p>https://github.com/android/platform_frameworks_base/commit/719c44e03b97e850a46136ba336d729f5fbd1f47
4844 */
4945 private static final int CPU_COUNT = Runtime .getRuntime ().availableProcessors ();
5046 /* package */ static final int CORE_POOL_SIZE = CPU_COUNT + 1 ;
@@ -56,59 +52,63 @@ private AndroidExecutors() {
5652 }
5753
5854 /**
59- * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available
60- * or create new threads until the core pool is full. tasks will then be queued. If an
61- * task cannot be queued, a new thread will be created unless this would exceed max pool
62- * size, then the task will be rejected. Threads will time out after 1 second.
63- * <p>
64- * Core thread timeout is only available on android-9+.
55+ * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available or create
56+ * new threads until the core pool is full. tasks will then be queued. If an task cannot be
57+ * queued, a new thread will be created unless this would exceed max pool size, then the task
58+ * will be rejected. Threads will time out after 1 second.
59+ *
60+ * <p> Core thread timeout is only available on android-9+.
6561 *
6662 * @return the newly created thread pool
6763 */
6864 public static ExecutorService newCachedThreadPool () {
69- ThreadPoolExecutor executor = new ThreadPoolExecutor (
70- CORE_POOL_SIZE ,
71- MAX_POOL_SIZE ,
72- KEEP_ALIVE_TIME , TimeUnit .SECONDS ,
73- new LinkedBlockingQueue <>());
65+ ThreadPoolExecutor executor =
66+ new ThreadPoolExecutor (
67+ CORE_POOL_SIZE ,
68+ MAX_POOL_SIZE ,
69+ KEEP_ALIVE_TIME ,
70+ TimeUnit .SECONDS ,
71+ new LinkedBlockingQueue <>());
7472
7573 allowCoreThreadTimeout (executor , true );
7674
7775 return executor ;
7876 }
7977
8078 /**
81- * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available
82- * or create new threads until the core pool is full. tasks will then be queued. If an
83- * task cannot be queued, a new thread will be created unless this would exceed max pool
84- * size, then the task will be rejected. Threads will time out after 1 second.
85- * <p>
86- * Core thread timeout is only available on android-9+.
79+ * Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available or create
80+ * new threads until the core pool is full. tasks will then be queued. If an task cannot be
81+ * queued, a new thread will be created unless this would exceed max pool size, then the task
82+ * will be rejected. Threads will time out after 1 second.
83+ *
84+ * <p> Core thread timeout is only available on android-9+.
8785 *
8886 * @param threadFactory the factory to use when creating new threads
8987 * @return the newly created thread pool
9088 */
9189 public static ExecutorService newCachedThreadPool (ThreadFactory threadFactory ) {
92- ThreadPoolExecutor executor = new ThreadPoolExecutor (
93- CORE_POOL_SIZE ,
94- MAX_POOL_SIZE ,
95- KEEP_ALIVE_TIME , TimeUnit .SECONDS ,
96- new LinkedBlockingQueue <>(),
97- threadFactory );
90+ ThreadPoolExecutor executor =
91+ new ThreadPoolExecutor (
92+ CORE_POOL_SIZE ,
93+ MAX_POOL_SIZE ,
94+ KEEP_ALIVE_TIME ,
95+ TimeUnit .SECONDS ,
96+ new LinkedBlockingQueue <>(),
97+ threadFactory );
9898
9999 allowCoreThreadTimeout (executor , true );
100100
101101 return executor ;
102102 }
103103
104104 /**
105- * Compatibility helper function for
106- * {@link java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)}
107- * <p>
108- * Only available on android-9+.
105+ * Compatibility helper function for {@link
106+ * java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)}
107+ *
108+ * <p> Only available on android-9+.
109109 *
110110 * @param executor the {@link java.util.concurrent.ThreadPoolExecutor}
111- * @param value true if should time out, else false
111+ * @param value true if should time out, else false
112112 */
113113 @ SuppressLint ("NewApi" )
114114 public static void allowCoreThreadTimeout (ThreadPoolExecutor executor , boolean value ) {
@@ -117,16 +117,12 @@ public static void allowCoreThreadTimeout(ThreadPoolExecutor executor, boolean v
117117 }
118118 }
119119
120- /**
121- * An {@link java.util.concurrent.Executor} that executes tasks on the UI thread.
122- */
120+ /** An {@link java.util.concurrent.Executor} that executes tasks on the UI thread. */
123121 public static Executor uiThread () {
124122 return INSTANCE .uiThread ;
125123 }
126124
127- /**
128- * An {@link java.util.concurrent.Executor} that runs tasks on the UI thread.
129- */
125+ /** An {@link java.util.concurrent.Executor} that runs tasks on the UI thread. */
130126 private static class UIThreadExecutor implements Executor {
131127 @ Override
132128 public void execute (Runnable command ) {
0 commit comments