This repository was 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
/
Copy pathBoltsExecutors.java
120 lines (107 loc) · 3.37 KB
/
BoltsExecutors.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
/*
* 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 java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Collection of {@link Executor}s to use in conjunction with {@link Task}.
*/
/* package */ final class BoltsExecutors {
private static final BoltsExecutors INSTANCE = new BoltsExecutors();
private static boolean isAndroidRuntime() {
String javaRuntimeName = System.getProperty("java.runtime.name");
if (javaRuntimeName == null) {
return false;
}
return javaRuntimeName.toLowerCase(Locale.US).contains("android");
}
private final ExecutorService background;
private final ScheduledExecutorService scheduled;
private final Executor immediate;
private BoltsExecutors() {
background = !isAndroidRuntime()
? java.util.concurrent.Executors.newCachedThreadPool()
: AndroidExecutors.newCachedThreadPool();
scheduled = Executors.newSingleThreadScheduledExecutor();
immediate = new ImmediateExecutor();
}
/**
* An {@link java.util.concurrent.Executor} that executes tasks in parallel.
*/
public static ExecutorService background() {
return INSTANCE.background;
}
/* package */ static ScheduledExecutorService scheduled() {
return INSTANCE.scheduled;
}
/**
* An {@link java.util.concurrent.Executor} that executes tasks in the current thread unless
* the stack runs too deep, at which point it will delegate to {@link BoltsExecutors#background}
* in order to trim the stack.
*/
/* package */ static Executor immediate() {
return INSTANCE.immediate;
}
/**
* An {@link java.util.concurrent.Executor} that runs a runnable inline (rather than scheduling it
* on a thread pool) as long as the recursion depth is less than MAX_DEPTH. If the executor has
* recursed too deeply, it will instead delegate to the {@link Task#BACKGROUND_EXECUTOR} in order
* to trim the stack.
*/
private static class ImmediateExecutor implements Executor {
private static final int MAX_DEPTH = 15;
private ThreadLocal<Integer> executionDepth = new ThreadLocal<>();
/**
* Increments the depth.
*
* @return the new depth value.
*/
private int incrementDepth() {
Integer oldDepth = executionDepth.get();
if (oldDepth == null) {
oldDepth = 0;
}
int newDepth = oldDepth + 1;
executionDepth.set(newDepth);
return newDepth;
}
/**
* Decrements the depth.
*
* @return the new depth value.
*/
private int decrementDepth() {
Integer oldDepth = executionDepth.get();
if (oldDepth == null) {
oldDepth = 0;
}
int newDepth = oldDepth - 1;
if (newDepth == 0) {
executionDepth.remove();
} else {
executionDepth.set(newDepth);
}
return newDepth;
}
@Override
public void execute(Runnable command) {
int depth = incrementDepth();
try {
if (depth <= MAX_DEPTH) {
command.run();
} else {
BoltsExecutors.background().execute(command);
}
} finally {
decrementDepth();
}
}
}
}