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