10
10
import android .os .Build ;
11
11
import android .os .Handler ;
12
12
import android .os .Looper ;
13
-
14
13
import java .util .concurrent .Executor ;
15
14
import java .util .concurrent .ExecutorService ;
16
15
import java .util .concurrent .LinkedBlockingQueue ;
21
20
/**
22
21
* This was created because the helper methods in {@link java.util.concurrent.Executors} do not work
23
22
* 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,
26
25
* queue them when the pool is full, and kill threads when they've been inactive for a certain
27
26
* period of time. This is not how {@link java.util.concurrent.Executors#newCachedThreadPool()}
28
27
* 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
33
32
* amount of threads.
34
33
*/
35
34
/* package */ final class AndroidExecutors {
36
35
37
36
/* package */ static final long KEEP_ALIVE_TIME = 1L ;
38
37
private static final AndroidExecutors INSTANCE = new AndroidExecutors ();
39
38
/**
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
48
44
*/
49
45
private static final int CPU_COUNT = Runtime .getRuntime ().availableProcessors ();
50
46
/* package */ static final int CORE_POOL_SIZE = CPU_COUNT + 1 ;
@@ -56,59 +52,63 @@ private AndroidExecutors() {
56
52
}
57
53
58
54
/**
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+.
65
61
*
66
62
* @return the newly created thread pool
67
63
*/
68
64
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 <>());
74
72
75
73
allowCoreThreadTimeout (executor , true );
76
74
77
75
return executor ;
78
76
}
79
77
80
78
/**
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+.
87
85
*
88
86
* @param threadFactory the factory to use when creating new threads
89
87
* @return the newly created thread pool
90
88
*/
91
89
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 );
98
98
99
99
allowCoreThreadTimeout (executor , true );
100
100
101
101
return executor ;
102
102
}
103
103
104
104
/**
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+.
109
109
*
110
110
* @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
112
112
*/
113
113
@ SuppressLint ("NewApi" )
114
114
public static void allowCoreThreadTimeout (ThreadPoolExecutor executor , boolean value ) {
@@ -117,16 +117,12 @@ public static void allowCoreThreadTimeout(ThreadPoolExecutor executor, boolean v
117
117
}
118
118
}
119
119
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. */
123
121
public static Executor uiThread () {
124
122
return INSTANCE .uiThread ;
125
123
}
126
124
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. */
130
126
private static class UIThreadExecutor implements Executor {
131
127
@ Override
132
128
public void execute (Runnable command ) {
0 commit comments