Repository navigation
Expand file tree
/
Copy pathThreads.java
More file actions
44 lines (38 loc) · 1.5 KB
/
Copy pathThreads.java
File metadata and controls
44 lines (38 loc) · 1.5 KB
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
package cn.reactnative.modules.update;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** The SDK's named worker threads, so they are identifiable in thread dumps / ANR traces. */
final class Threads {
private Threads() {
}
/** A single-thread executor: tasks run serialized in submission order. */
static ExecutorService single(final String name, final boolean daemon) {
return Executors.newSingleThreadExecutor(runnable -> {
Thread thread = new Thread(runnable, name);
thread.setDaemon(daemon);
return thread;
});
}
/** Daemon threads on demand, reused while idle: concurrent tasks never queue. */
static ExecutorService cached(final String name) {
return Executors.newCachedThreadPool(runnable -> {
Thread thread = new Thread(runnable, name);
thread.setDaemon(true);
return thread;
});
}
/** Starts a one-off daemon thread: it never keeps a dying process alive. */
static Thread startDaemon(String name, Runnable task) {
return startDaemon(name, -1, task);
}
/** As above, with an explicit priority (negative keeps the inherited one). */
static Thread startDaemon(String name, int priority, Runnable task) {
Thread thread = new Thread(task, name);
if (priority >= 0) {
thread.setPriority(priority);
}
thread.setDaemon(true);
thread.start();
return thread;
}
}