1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.136 |
|
9 |
dl |
1.192 |
import java.lang.invoke.MethodHandles; |
10 |
|
|
import java.lang.invoke.VarHandle; |
11 |
jsr166 |
1.136 |
import java.util.concurrent.locks.LockSupport; |
12 |
|
|
import java.util.function.BiConsumer; |
13 |
|
|
import java.util.function.BiFunction; |
14 |
dl |
1.34 |
import java.util.function.Consumer; |
15 |
dl |
1.1 |
import java.util.function.Function; |
16 |
jsr166 |
1.136 |
import java.util.function.Supplier; |
17 |
dl |
1.1 |
|
18 |
|
|
/** |
19 |
|
|
* A {@link Future} that may be explicitly completed (setting its |
20 |
dl |
1.88 |
* value and status), and may be used as a {@link CompletionStage}, |
21 |
|
|
* supporting dependent functions and actions that trigger upon its |
22 |
|
|
* completion. |
23 |
dl |
1.1 |
* |
24 |
jsr166 |
1.50 |
* <p>When two or more threads attempt to |
25 |
|
|
* {@link #complete complete}, |
26 |
jsr166 |
1.52 |
* {@link #completeExceptionally completeExceptionally}, or |
27 |
jsr166 |
1.50 |
* {@link #cancel cancel} |
28 |
|
|
* a CompletableFuture, only one of them succeeds. |
29 |
dl |
1.19 |
* |
30 |
dl |
1.91 |
* <p>In addition to these and related methods for directly |
31 |
|
|
* manipulating status and results, CompletableFuture implements |
32 |
|
|
* interface {@link CompletionStage} with the following policies: <ul> |
33 |
dl |
1.35 |
* |
34 |
dl |
1.88 |
* <li>Actions supplied for dependent completions of |
35 |
|
|
* <em>non-async</em> methods may be performed by the thread that |
36 |
|
|
* completes the current CompletableFuture, or by any other caller of |
37 |
jsr166 |
1.169 |
* a completion method. |
38 |
jsr166 |
1.65 |
* |
39 |
dl |
1.88 |
* <li>All <em>async</em> methods without an explicit Executor |
40 |
dl |
1.96 |
* argument are performed using the {@link ForkJoinPool#commonPool()} |
41 |
|
|
* (unless it does not support a parallelism level of at least two, in |
42 |
dl |
1.143 |
* which case, a new Thread is created to run each task). This may be |
43 |
|
|
* overridden for non-static methods in subclasses by defining method |
44 |
|
|
* {@link #defaultExecutor()}. To simplify monitoring, debugging, |
45 |
|
|
* and tracking, all generated asynchronous tasks are instances of the |
46 |
|
|
* marker interface {@link AsynchronousCompletionTask}. Operations |
47 |
jsr166 |
1.160 |
* with time-delays can use adapter methods defined in this class, for |
48 |
dl |
1.143 |
* example: {@code supplyAsync(supplier, delayedExecutor(timeout, |
49 |
|
|
* timeUnit))}. To support methods with delays and timeouts, this |
50 |
|
|
* class maintains at most one daemon thread for triggering and |
51 |
jsr166 |
1.169 |
* cancelling actions, not for running them. |
52 |
dl |
1.35 |
* |
53 |
dl |
1.88 |
* <li>All CompletionStage methods are implemented independently of |
54 |
|
|
* other public methods, so the behavior of one method is not impacted |
55 |
jsr166 |
1.169 |
* by overrides of others in subclasses. |
56 |
dl |
1.143 |
* |
57 |
|
|
* <li>All CompletionStage methods return CompletableFutures. To |
58 |
|
|
* restrict usages to only those methods defined in interface |
59 |
dl |
1.146 |
* CompletionStage, use method {@link #minimalCompletionStage}. Or to |
60 |
|
|
* ensure only that clients do not themselves modify a future, use |
61 |
jsr166 |
1.169 |
* method {@link #copy}. |
62 |
|
|
* </ul> |
63 |
dl |
1.88 |
* |
64 |
dl |
1.91 |
* <p>CompletableFuture also implements {@link Future} with the following |
65 |
dl |
1.88 |
* policies: <ul> |
66 |
|
|
* |
67 |
|
|
* <li>Since (unlike {@link FutureTask}) this class has no direct |
68 |
jsr166 |
1.55 |
* control over the computation that causes it to be completed, |
69 |
dl |
1.88 |
* cancellation is treated as just another form of exceptional |
70 |
|
|
* completion. Method {@link #cancel cancel} has the same effect as |
71 |
|
|
* {@code completeExceptionally(new CancellationException())}. Method |
72 |
|
|
* {@link #isCompletedExceptionally} can be used to determine if a |
73 |
jsr166 |
1.169 |
* CompletableFuture completed in any exceptional fashion. |
74 |
jsr166 |
1.55 |
* |
75 |
dl |
1.88 |
* <li>In case of exceptional completion with a CompletionException, |
76 |
jsr166 |
1.55 |
* methods {@link #get()} and {@link #get(long, TimeUnit)} throw an |
77 |
|
|
* {@link ExecutionException} with the same cause as held in the |
78 |
dl |
1.88 |
* corresponding CompletionException. To simplify usage in most |
79 |
|
|
* contexts, this class also defines methods {@link #join()} and |
80 |
|
|
* {@link #getNow} that instead throw the CompletionException directly |
81 |
jsr166 |
1.169 |
* in these cases. |
82 |
|
|
* </ul> |
83 |
jsr166 |
1.80 |
* |
84 |
dl |
1.164 |
* <p>Arguments used to pass a completion result (that is, for |
85 |
|
|
* parameters of type {@code T}) for methods accepting them may be |
86 |
|
|
* null, but passing a null value for any other parameter will result |
87 |
|
|
* in a {@link NullPointerException} being thrown. |
88 |
|
|
* |
89 |
dl |
1.143 |
* <p>Subclasses of this class should normally override the "virtual |
90 |
|
|
* constructor" method {@link #newIncompleteFuture}, which establishes |
91 |
|
|
* the concrete type returned by CompletionStage methods. For example, |
92 |
|
|
* here is a class that substitutes a different default Executor and |
93 |
|
|
* disables the {@code obtrude} methods: |
94 |
|
|
* |
95 |
|
|
* <pre> {@code |
96 |
|
|
* class MyCompletableFuture<T> extends CompletableFuture<T> { |
97 |
|
|
* static final Executor myExecutor = ...; |
98 |
|
|
* public MyCompletableFuture() { } |
99 |
|
|
* public <U> CompletableFuture<U> newIncompleteFuture() { |
100 |
|
|
* return new MyCompletableFuture<U>(); } |
101 |
|
|
* public Executor defaultExecutor() { |
102 |
|
|
* return myExecutor; } |
103 |
|
|
* public void obtrudeValue(T value) { |
104 |
|
|
* throw new UnsupportedOperationException(); } |
105 |
dl |
1.150 |
* public void obtrudeException(Throwable ex) { |
106 |
dl |
1.143 |
* throw new UnsupportedOperationException(); } |
107 |
|
|
* }}</pre> |
108 |
|
|
* |
109 |
dl |
1.1 |
* @author Doug Lea |
110 |
jsr166 |
1.156 |
* @param <T> The result type returned by this future's {@code join} |
111 |
|
|
* and {@code get} methods |
112 |
jsr166 |
1.208 |
* @since 1.8 |
113 |
dl |
1.1 |
*/ |
114 |
dl |
1.88 |
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> { |
115 |
dl |
1.28 |
|
116 |
dl |
1.1 |
/* |
117 |
dl |
1.20 |
* Overview: |
118 |
dl |
1.1 |
* |
119 |
dl |
1.104 |
* A CompletableFuture may have dependent completion actions, |
120 |
|
|
* collected in a linked stack. It atomically completes by CASing |
121 |
|
|
* a result field, and then pops off and runs those actions. This |
122 |
|
|
* applies across normal vs exceptional outcomes, sync vs async |
123 |
|
|
* actions, binary triggers, and various forms of completions. |
124 |
|
|
* |
125 |
jsr166 |
1.202 |
* Non-nullness of volatile field "result" indicates done. It may |
126 |
|
|
* be set directly if known to be thread-confined, else via CAS. |
127 |
|
|
* An AltResult is used to box null as a result, as well as to |
128 |
|
|
* hold exceptions. Using a single field makes completion simple |
129 |
|
|
* to detect and trigger. Result encoding and decoding is |
130 |
|
|
* straightforward but tedious and adds to the sprawl of trapping |
131 |
|
|
* and associating exceptions with targets. Minor simplifications |
132 |
|
|
* rely on (static) NIL (to box null results) being the only |
133 |
|
|
* AltResult with a null exception field, so we don't usually need |
134 |
|
|
* explicit comparisons. Even though some of the generics casts |
135 |
|
|
* are unchecked (see SuppressWarnings annotations), they are |
136 |
|
|
* placed to be appropriate even if checked. |
137 |
dl |
1.104 |
* |
138 |
|
|
* Dependent actions are represented by Completion objects linked |
139 |
dl |
1.113 |
* as Treiber stacks headed by field "stack". There are Completion |
140 |
jsr166 |
1.202 |
* classes for each kind of action, grouped into: |
141 |
|
|
* - single-input (UniCompletion), |
142 |
|
|
* - two-input (BiCompletion), |
143 |
|
|
* - projected (BiCompletions using exactly one of two inputs), |
144 |
|
|
* - shared (CoCompletion, used by the second of two sources), |
145 |
|
|
* - zero-input source actions, |
146 |
|
|
* - Signallers that unblock waiters. |
147 |
|
|
* Class Completion extends ForkJoinTask to enable async execution |
148 |
dl |
1.113 |
* (adding no space overhead because we exploit its "tag" methods |
149 |
|
|
* to maintain claims). It is also declared as Runnable to allow |
150 |
|
|
* usage with arbitrary executors. |
151 |
|
|
* |
152 |
|
|
* Support for each kind of CompletionStage relies on a separate |
153 |
|
|
* class, along with two CompletableFuture methods: |
154 |
|
|
* |
155 |
|
|
* * A Completion class with name X corresponding to function, |
156 |
|
|
* prefaced with "Uni", "Bi", or "Or". Each class contains |
157 |
|
|
* fields for source(s), actions, and dependent. They are |
158 |
|
|
* boringly similar, differing from others only with respect to |
159 |
|
|
* underlying functional forms. We do this so that users don't |
160 |
jsr166 |
1.160 |
* encounter layers of adapters in common usages. |
161 |
dl |
1.113 |
* |
162 |
|
|
* * Boolean CompletableFuture method x(...) (for example |
163 |
jsr166 |
1.202 |
* biApply) takes all of the arguments needed to check that an |
164 |
dl |
1.113 |
* action is triggerable, and then either runs the action or |
165 |
|
|
* arranges its async execution by executing its Completion |
166 |
|
|
* argument, if present. The method returns true if known to be |
167 |
|
|
* complete. |
168 |
|
|
* |
169 |
|
|
* * Completion method tryFire(int mode) invokes the associated x |
170 |
|
|
* method with its held arguments, and on success cleans up. |
171 |
jsr166 |
1.130 |
* The mode argument allows tryFire to be called twice (SYNC, |
172 |
|
|
* then ASYNC); the first to screen and trap exceptions while |
173 |
jsr166 |
1.199 |
* arranging to execute, and the second when called from a task. |
174 |
|
|
* (A few classes are not used async so take slightly different |
175 |
|
|
* forms.) The claim() callback suppresses function invocation |
176 |
|
|
* if already claimed by another thread. |
177 |
dl |
1.113 |
* |
178 |
jsr166 |
1.202 |
* * Some classes (for example UniApply) have separate handling |
179 |
|
|
* code for when known to be thread-confined ("now" methods) and |
180 |
|
|
* for when shared (in tryFire), for efficiency. |
181 |
|
|
* |
182 |
dl |
1.113 |
* * CompletableFuture method xStage(...) is called from a public |
183 |
jsr166 |
1.202 |
* stage method of CompletableFuture f. It screens user |
184 |
dl |
1.113 |
* arguments and invokes and/or creates the stage object. If |
185 |
jsr166 |
1.202 |
* not async and already triggerable, the action is run |
186 |
|
|
* immediately. Otherwise a Completion c is created, and |
187 |
|
|
* submitted to the executor if triggerable, or pushed onto f's |
188 |
|
|
* stack if not. Completion actions are started via c.tryFire. |
189 |
|
|
* We recheck after pushing to a source future's stack to cover |
190 |
|
|
* possible races if the source completes while pushing. |
191 |
|
|
* Classes with two inputs (for example BiApply) deal with races |
192 |
|
|
* across both while pushing actions. The second completion is |
193 |
|
|
* a CoCompletion pointing to the first, shared so that at most |
194 |
|
|
* one performs the action. The multiple-arity methods allOf |
195 |
jsr166 |
1.207 |
* does this pairwise to form trees of completions. Method |
196 |
|
|
* anyOf is handled differently from allOf because completion of |
197 |
|
|
* any source should trigger a cleanStack of other sources. |
198 |
|
|
* Each AnyOf completion can reach others via a shared array. |
199 |
dl |
1.113 |
* |
200 |
|
|
* Note that the generic type parameters of methods vary according |
201 |
|
|
* to whether "this" is a source, dependent, or completion. |
202 |
|
|
* |
203 |
|
|
* Method postComplete is called upon completion unless the target |
204 |
|
|
* is guaranteed not to be observable (i.e., not yet returned or |
205 |
|
|
* linked). Multiple threads can call postComplete, which |
206 |
|
|
* atomically pops each dependent action, and tries to trigger it |
207 |
jsr166 |
1.130 |
* via method tryFire, in NESTED mode. Triggering can propagate |
208 |
|
|
* recursively, so NESTED mode returns its completed dependent (if |
209 |
|
|
* one exists) for further processing by its caller (see method |
210 |
|
|
* postFire). |
211 |
dl |
1.104 |
* |
212 |
|
|
* Blocking methods get() and join() rely on Signaller Completions |
213 |
|
|
* that wake up waiting threads. The mechanics are similar to |
214 |
|
|
* Treiber stack wait-nodes used in FutureTask, Phaser, and |
215 |
|
|
* SynchronousQueue. See their internal documentation for |
216 |
|
|
* algorithmic details. |
217 |
|
|
* |
218 |
|
|
* Without precautions, CompletableFutures would be prone to |
219 |
dl |
1.113 |
* garbage accumulation as chains of Completions build up, each |
220 |
|
|
* pointing back to its sources. So we null out fields as soon as |
221 |
jsr166 |
1.181 |
* possible. The screening checks needed anyway harmlessly ignore |
222 |
|
|
* null arguments that may have been obtained during races with |
223 |
dl |
1.185 |
* threads nulling out fields. We also try to unlink non-isLive |
224 |
|
|
* (fired or cancelled) Completions from stacks that might |
225 |
|
|
* otherwise never be popped: Method cleanStack always unlinks non |
226 |
|
|
* isLive completions from the head of stack; others may |
227 |
|
|
* occasionally remain if racing with other cancellations or |
228 |
|
|
* removals. |
229 |
|
|
* |
230 |
|
|
* Completion fields need not be declared as final or volatile |
231 |
|
|
* because they are only visible to other threads upon safe |
232 |
|
|
* publication. |
233 |
dl |
1.104 |
*/ |
234 |
|
|
|
235 |
dl |
1.113 |
volatile Object result; // Either the result or boxed AltResult |
236 |
|
|
volatile Completion stack; // Top of Treiber stack of dependent actions |
237 |
dl |
1.104 |
|
238 |
|
|
final boolean internalComplete(Object r) { // CAS from null to r |
239 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, r); |
240 |
dl |
1.104 |
} |
241 |
|
|
|
242 |
jsr166 |
1.129 |
/** Returns true if successfully pushed c onto stack. */ |
243 |
|
|
final boolean tryPushStack(Completion c) { |
244 |
|
|
Completion h = stack; |
245 |
jsr166 |
1.196 |
NEXT.set(c, h); // CAS piggyback |
246 |
dl |
1.192 |
return STACK.compareAndSet(this, h, c); |
247 |
jsr166 |
1.129 |
} |
248 |
|
|
|
249 |
|
|
/** Unconditionally pushes c onto stack, retrying if necessary. */ |
250 |
|
|
final void pushStack(Completion c) { |
251 |
|
|
do {} while (!tryPushStack(c)); |
252 |
|
|
} |
253 |
|
|
|
254 |
dl |
1.104 |
/* ------------- Encoding and decoding outcomes -------------- */ |
255 |
|
|
|
256 |
|
|
static final class AltResult { // See above |
257 |
|
|
final Throwable ex; // null only for NIL |
258 |
|
|
AltResult(Throwable x) { this.ex = x; } |
259 |
dl |
1.1 |
} |
260 |
|
|
|
261 |
jsr166 |
1.128 |
/** The encoding of the null value. */ |
262 |
dl |
1.1 |
static final AltResult NIL = new AltResult(null); |
263 |
|
|
|
264 |
jsr166 |
1.128 |
/** Completes with the null value, unless already completed. */ |
265 |
|
|
final boolean completeNull() { |
266 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, NIL); |
267 |
jsr166 |
1.128 |
} |
268 |
|
|
|
269 |
|
|
/** Returns the encoding of the given non-exceptional value. */ |
270 |
|
|
final Object encodeValue(T t) { |
271 |
|
|
return (t == null) ? NIL : t; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
/** Completes with a non-exceptional result, unless already completed. */ |
275 |
|
|
final boolean completeValue(T t) { |
276 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, (t == null) ? NIL : t); |
277 |
jsr166 |
1.128 |
} |
278 |
|
|
|
279 |
dl |
1.20 |
/** |
280 |
dl |
1.104 |
* Returns the encoding of the given (non-null) exception as a |
281 |
|
|
* wrapped CompletionException unless it is one already. |
282 |
dl |
1.99 |
*/ |
283 |
dl |
1.113 |
static AltResult encodeThrowable(Throwable x) { |
284 |
dl |
1.104 |
return new AltResult((x instanceof CompletionException) ? x : |
285 |
|
|
new CompletionException(x)); |
286 |
dl |
1.99 |
} |
287 |
|
|
|
288 |
jsr166 |
1.128 |
/** Completes with an exceptional result, unless already completed. */ |
289 |
|
|
final boolean completeThrowable(Throwable x) { |
290 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, encodeThrowable(x)); |
291 |
jsr166 |
1.128 |
} |
292 |
|
|
|
293 |
|
|
/** |
294 |
|
|
* Returns the encoding of the given (non-null) exception as a |
295 |
|
|
* wrapped CompletionException unless it is one already. May |
296 |
|
|
* return the given Object r (which must have been the result of a |
297 |
|
|
* source future) if it is equivalent, i.e. if this is a simple |
298 |
|
|
* relay of an existing CompletionException. |
299 |
|
|
*/ |
300 |
|
|
static Object encodeThrowable(Throwable x, Object r) { |
301 |
|
|
if (!(x instanceof CompletionException)) |
302 |
|
|
x = new CompletionException(x); |
303 |
|
|
else if (r instanceof AltResult && x == ((AltResult)r).ex) |
304 |
|
|
return r; |
305 |
|
|
return new AltResult(x); |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
/** |
309 |
|
|
* Completes with the given (non-null) exceptional result as a |
310 |
|
|
* wrapped CompletionException unless it is one already, unless |
311 |
|
|
* already completed. May complete with the given Object r |
312 |
|
|
* (which must have been the result of a source future) if it is |
313 |
|
|
* equivalent, i.e. if this is a simple propagation of an |
314 |
|
|
* existing CompletionException. |
315 |
|
|
*/ |
316 |
|
|
final boolean completeThrowable(Throwable x, Object r) { |
317 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, encodeThrowable(x, r)); |
318 |
jsr166 |
1.128 |
} |
319 |
|
|
|
320 |
dl |
1.99 |
/** |
321 |
dl |
1.104 |
* Returns the encoding of the given arguments: if the exception |
322 |
dl |
1.113 |
* is non-null, encodes as AltResult. Otherwise uses the given |
323 |
dl |
1.104 |
* value, boxed as NIL if null. |
324 |
dl |
1.99 |
*/ |
325 |
jsr166 |
1.127 |
Object encodeOutcome(T t, Throwable x) { |
326 |
|
|
return (x == null) ? (t == null) ? NIL : t : encodeThrowable(x); |
327 |
dl |
1.20 |
} |
328 |
|
|
|
329 |
dl |
1.1 |
/** |
330 |
jsr166 |
1.115 |
* Returns the encoding of a copied outcome; if exceptional, |
331 |
dl |
1.113 |
* rewraps as a CompletionException, else returns argument. |
332 |
dl |
1.1 |
*/ |
333 |
dl |
1.113 |
static Object encodeRelay(Object r) { |
334 |
|
|
Throwable x; |
335 |
jsr166 |
1.191 |
if (r instanceof AltResult |
336 |
|
|
&& (x = ((AltResult)r).ex) != null |
337 |
|
|
&& !(x instanceof CompletionException)) |
338 |
|
|
r = new AltResult(new CompletionException(x)); |
339 |
|
|
return r; |
340 |
dl |
1.1 |
} |
341 |
|
|
|
342 |
|
|
/** |
343 |
jsr166 |
1.128 |
* Completes with r or a copy of r, unless already completed. |
344 |
|
|
* If exceptional, r is first coerced to a CompletionException. |
345 |
|
|
*/ |
346 |
|
|
final boolean completeRelay(Object r) { |
347 |
jsr166 |
1.194 |
return RESULT.compareAndSet(this, null, encodeRelay(r)); |
348 |
jsr166 |
1.128 |
} |
349 |
|
|
|
350 |
|
|
/** |
351 |
jsr166 |
1.108 |
* Reports result using Future.get conventions. |
352 |
dl |
1.1 |
*/ |
353 |
jsr166 |
1.190 |
private static Object reportGet(Object r) |
354 |
dl |
1.104 |
throws InterruptedException, ExecutionException { |
355 |
|
|
if (r == null) // by convention below, null means interrupted |
356 |
|
|
throw new InterruptedException(); |
357 |
|
|
if (r instanceof AltResult) { |
358 |
|
|
Throwable x, cause; |
359 |
|
|
if ((x = ((AltResult)r).ex) == null) |
360 |
dl |
1.28 |
return null; |
361 |
dl |
1.104 |
if (x instanceof CancellationException) |
362 |
|
|
throw (CancellationException)x; |
363 |
|
|
if ((x instanceof CompletionException) && |
364 |
|
|
(cause = x.getCause()) != null) |
365 |
|
|
x = cause; |
366 |
|
|
throw new ExecutionException(x); |
367 |
dl |
1.28 |
} |
368 |
jsr166 |
1.190 |
return r; |
369 |
dl |
1.1 |
} |
370 |
|
|
|
371 |
dl |
1.113 |
/** |
372 |
|
|
* Decodes outcome to return result or throw unchecked exception. |
373 |
|
|
*/ |
374 |
jsr166 |
1.190 |
private static Object reportJoin(Object r) { |
375 |
dl |
1.113 |
if (r instanceof AltResult) { |
376 |
|
|
Throwable x; |
377 |
|
|
if ((x = ((AltResult)r).ex) == null) |
378 |
|
|
return null; |
379 |
|
|
if (x instanceof CancellationException) |
380 |
|
|
throw (CancellationException)x; |
381 |
|
|
if (x instanceof CompletionException) |
382 |
|
|
throw (CompletionException)x; |
383 |
|
|
throw new CompletionException(x); |
384 |
|
|
} |
385 |
jsr166 |
1.190 |
return r; |
386 |
dl |
1.113 |
} |
387 |
|
|
|
388 |
jsr166 |
1.123 |
/* ------------- Async task preliminaries -------------- */ |
389 |
dl |
1.104 |
|
390 |
dl |
1.1 |
/** |
391 |
jsr166 |
1.56 |
* A marker interface identifying asynchronous tasks produced by |
392 |
dl |
1.28 |
* {@code async} methods. This may be useful for monitoring, |
393 |
|
|
* debugging, and tracking asynchronous activities. |
394 |
jsr166 |
1.57 |
* |
395 |
|
|
* @since 1.8 |
396 |
dl |
1.1 |
*/ |
397 |
dl |
1.28 |
public static interface AsynchronousCompletionTask { |
398 |
dl |
1.1 |
} |
399 |
|
|
|
400 |
jsr166 |
1.171 |
private static final boolean USE_COMMON_POOL = |
401 |
jsr166 |
1.127 |
(ForkJoinPool.getCommonPoolParallelism() > 1); |
402 |
|
|
|
403 |
dl |
1.104 |
/** |
404 |
dl |
1.109 |
* Default executor -- ForkJoinPool.commonPool() unless it cannot |
405 |
|
|
* support parallelism. |
406 |
|
|
*/ |
407 |
jsr166 |
1.171 |
private static final Executor ASYNC_POOL = USE_COMMON_POOL ? |
408 |
dl |
1.109 |
ForkJoinPool.commonPool() : new ThreadPerTaskExecutor(); |
409 |
|
|
|
410 |
|
|
/** Fallback if ForkJoinPool.commonPool() cannot support parallelism */ |
411 |
|
|
static final class ThreadPerTaskExecutor implements Executor { |
412 |
|
|
public void execute(Runnable r) { new Thread(r).start(); } |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
/** |
416 |
|
|
* Null-checks user executor argument, and translates uses of |
417 |
jsr166 |
1.171 |
* commonPool to ASYNC_POOL in case parallelism disabled. |
418 |
dl |
1.109 |
*/ |
419 |
|
|
static Executor screenExecutor(Executor e) { |
420 |
jsr166 |
1.171 |
if (!USE_COMMON_POOL && e == ForkJoinPool.commonPool()) |
421 |
|
|
return ASYNC_POOL; |
422 |
dl |
1.109 |
if (e == null) throw new NullPointerException(); |
423 |
jsr166 |
1.127 |
return e; |
424 |
dl |
1.109 |
} |
425 |
|
|
|
426 |
jsr166 |
1.130 |
// Modes for Completion.tryFire. Signedness matters. |
427 |
dl |
1.113 |
static final int SYNC = 0; |
428 |
|
|
static final int ASYNC = 1; |
429 |
|
|
static final int NESTED = -1; |
430 |
dl |
1.104 |
|
431 |
dl |
1.113 |
/* ------------- Base Completion classes and operations -------------- */ |
432 |
|
|
|
433 |
|
|
@SuppressWarnings("serial") |
434 |
|
|
abstract static class Completion extends ForkJoinTask<Void> |
435 |
|
|
implements Runnable, AsynchronousCompletionTask { |
436 |
dl |
1.110 |
volatile Completion next; // Treiber stack link |
437 |
dl |
1.104 |
|
438 |
|
|
/** |
439 |
dl |
1.113 |
* Performs completion action if triggered, returning a |
440 |
|
|
* dependent that may need propagation, if one exists. |
441 |
|
|
* |
442 |
|
|
* @param mode SYNC, ASYNC, or NESTED |
443 |
dl |
1.104 |
*/ |
444 |
dl |
1.113 |
abstract CompletableFuture<?> tryFire(int mode); |
445 |
|
|
|
446 |
jsr166 |
1.116 |
/** Returns true if possibly still triggerable. Used by cleanStack. */ |
447 |
dl |
1.113 |
abstract boolean isLive(); |
448 |
|
|
|
449 |
|
|
public final void run() { tryFire(ASYNC); } |
450 |
dl |
1.158 |
public final boolean exec() { tryFire(ASYNC); return false; } |
451 |
dl |
1.113 |
public final Void getRawResult() { return null; } |
452 |
|
|
public final void setRawResult(Void v) {} |
453 |
jsr166 |
1.134 |
} |
454 |
jsr166 |
1.129 |
|
455 |
dl |
1.104 |
/** |
456 |
dl |
1.113 |
* Pops and tries to trigger all reachable dependents. Call only |
457 |
|
|
* when known to be done. |
458 |
dl |
1.104 |
*/ |
459 |
|
|
final void postComplete() { |
460 |
|
|
/* |
461 |
dl |
1.113 |
* On each step, variable f holds current dependents to pop |
462 |
dl |
1.104 |
* and run. It is extended along only one path at a time, |
463 |
dl |
1.113 |
* pushing others to avoid unbounded recursion. |
464 |
dl |
1.104 |
*/ |
465 |
dl |
1.110 |
CompletableFuture<?> f = this; Completion h; |
466 |
dl |
1.113 |
while ((h = f.stack) != null || |
467 |
|
|
(f != this && (h = (f = this).stack) != null)) { |
468 |
dl |
1.110 |
CompletableFuture<?> d; Completion t; |
469 |
dl |
1.192 |
if (STACK.compareAndSet(f, h, t = h.next)) { |
470 |
dl |
1.104 |
if (t != null) { |
471 |
jsr166 |
1.129 |
if (f != this) { |
472 |
|
|
pushStack(h); |
473 |
dl |
1.104 |
continue; |
474 |
|
|
} |
475 |
jsr166 |
1.195 |
NEXT.compareAndSet(h, t, null); // try to detach |
476 |
dl |
1.28 |
} |
477 |
dl |
1.113 |
f = (d = h.tryFire(NESTED)) == null ? this : d; |
478 |
dl |
1.19 |
} |
479 |
|
|
} |
480 |
|
|
} |
481 |
|
|
|
482 |
dl |
1.185 |
/** Traverses stack and unlinks one or more dead Completions, if found. */ |
483 |
dl |
1.113 |
final void cleanStack() { |
484 |
jsr166 |
1.205 |
Completion p = stack; |
485 |
|
|
// ensure head of stack live |
486 |
|
|
for (boolean unlinked = false;;) { |
487 |
|
|
if (p == null) |
488 |
|
|
return; |
489 |
|
|
else if (p.isLive()) { |
490 |
|
|
if (unlinked) |
491 |
|
|
return; |
492 |
|
|
else |
493 |
dl |
1.185 |
break; |
494 |
dl |
1.113 |
} |
495 |
jsr166 |
1.209 |
else if (STACK.weakCompareAndSet(this, p, (p = p.next))) |
496 |
jsr166 |
1.205 |
unlinked = true; |
497 |
|
|
else |
498 |
|
|
p = stack; |
499 |
|
|
} |
500 |
|
|
// try to unlink first non-live |
501 |
|
|
for (Completion q = p.next; q != null;) { |
502 |
|
|
Completion s = q.next; |
503 |
|
|
if (q.isLive()) { |
504 |
|
|
p = q; |
505 |
|
|
q = s; |
506 |
jsr166 |
1.209 |
} else if (NEXT.weakCompareAndSet(p, q, s)) |
507 |
jsr166 |
1.205 |
break; |
508 |
|
|
else |
509 |
|
|
q = p.next; |
510 |
dl |
1.113 |
} |
511 |
|
|
} |
512 |
|
|
|
513 |
|
|
/* ------------- One-input Completions -------------- */ |
514 |
dl |
1.104 |
|
515 |
dl |
1.113 |
/** A Completion with a source, dependent, and executor. */ |
516 |
|
|
@SuppressWarnings("serial") |
517 |
jsr166 |
1.124 |
abstract static class UniCompletion<T,V> extends Completion { |
518 |
dl |
1.113 |
Executor executor; // executor to use (null if none) |
519 |
jsr166 |
1.124 |
CompletableFuture<V> dep; // the dependent to complete |
520 |
|
|
CompletableFuture<T> src; // source for action |
521 |
dl |
1.104 |
|
522 |
jsr166 |
1.124 |
UniCompletion(Executor executor, CompletableFuture<V> dep, |
523 |
|
|
CompletableFuture<T> src) { |
524 |
dl |
1.113 |
this.executor = executor; this.dep = dep; this.src = src; |
525 |
dl |
1.104 |
} |
526 |
|
|
|
527 |
dl |
1.113 |
/** |
528 |
|
|
* Returns true if action can be run. Call only when known to |
529 |
|
|
* be triggerable. Uses FJ tag bit to ensure that only one |
530 |
|
|
* thread claims ownership. If async, starts as task -- a |
531 |
|
|
* later call to tryFire will run action. |
532 |
|
|
*/ |
533 |
|
|
final boolean claim() { |
534 |
|
|
Executor e = executor; |
535 |
|
|
if (compareAndSetForkJoinTaskTag((short)0, (short)1)) { |
536 |
|
|
if (e == null) |
537 |
|
|
return true; |
538 |
|
|
executor = null; // disable |
539 |
|
|
e.execute(this); |
540 |
|
|
} |
541 |
|
|
return false; |
542 |
dl |
1.104 |
} |
543 |
|
|
|
544 |
dl |
1.113 |
final boolean isLive() { return dep != null; } |
545 |
dl |
1.1 |
} |
546 |
|
|
|
547 |
jsr166 |
1.188 |
/** |
548 |
|
|
* Pushes the given completion unless it completes while trying. |
549 |
jsr166 |
1.198 |
* Caller should first check that result is null. |
550 |
jsr166 |
1.188 |
*/ |
551 |
jsr166 |
1.198 |
final void unipush(Completion c) { |
552 |
dl |
1.104 |
if (c != null) { |
553 |
jsr166 |
1.188 |
while (!tryPushStack(c)) { |
554 |
|
|
if (result != null) { |
555 |
jsr166 |
1.196 |
NEXT.set(c, null); |
556 |
jsr166 |
1.188 |
break; |
557 |
|
|
} |
558 |
|
|
} |
559 |
|
|
if (result != null) |
560 |
|
|
c.tryFire(SYNC); |
561 |
dl |
1.104 |
} |
562 |
|
|
} |
563 |
|
|
|
564 |
dl |
1.113 |
/** |
565 |
jsr166 |
1.206 |
* Post-processing by dependent after successful UniCompletion tryFire. |
566 |
|
|
* Tries to clean stack of source a, and then either runs postComplete |
567 |
|
|
* or returns this to caller, depending on mode. |
568 |
dl |
1.113 |
*/ |
569 |
|
|
final CompletableFuture<T> postFire(CompletableFuture<?> a, int mode) { |
570 |
|
|
if (a != null && a.stack != null) { |
571 |
dl |
1.185 |
Object r; |
572 |
|
|
if ((r = a.result) == null) |
573 |
dl |
1.113 |
a.cleanStack(); |
574 |
dl |
1.185 |
if (mode >= 0 && (r != null || a.result != null)) |
575 |
dl |
1.113 |
a.postComplete(); |
576 |
dl |
1.1 |
} |
577 |
dl |
1.113 |
if (result != null && stack != null) { |
578 |
|
|
if (mode < 0) |
579 |
|
|
return this; |
580 |
|
|
else |
581 |
|
|
postComplete(); |
582 |
dl |
1.1 |
} |
583 |
dl |
1.113 |
return null; |
584 |
dl |
1.1 |
} |
585 |
|
|
|
586 |
dl |
1.113 |
@SuppressWarnings("serial") |
587 |
jsr166 |
1.124 |
static final class UniApply<T,V> extends UniCompletion<T,V> { |
588 |
|
|
Function<? super T,? extends V> fn; |
589 |
|
|
UniApply(Executor executor, CompletableFuture<V> dep, |
590 |
|
|
CompletableFuture<T> src, |
591 |
|
|
Function<? super T,? extends V> fn) { |
592 |
dl |
1.113 |
super(executor, dep, src); this.fn = fn; |
593 |
|
|
} |
594 |
jsr166 |
1.124 |
final CompletableFuture<V> tryFire(int mode) { |
595 |
|
|
CompletableFuture<V> d; CompletableFuture<T> a; |
596 |
jsr166 |
1.200 |
Object r; Throwable x; Function<? super T,? extends V> f; |
597 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
598 |
|
|
|| (a = src) == null || (r = a.result) == null) |
599 |
dl |
1.113 |
return null; |
600 |
jsr166 |
1.200 |
tryComplete: if (d.result == null) { |
601 |
|
|
if (r instanceof AltResult) { |
602 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
603 |
|
|
d.completeThrowable(x, r); |
604 |
|
|
break tryComplete; |
605 |
|
|
} |
606 |
|
|
r = null; |
607 |
|
|
} |
608 |
|
|
try { |
609 |
|
|
if (mode <= 0 && !claim()) |
610 |
|
|
return null; |
611 |
|
|
else { |
612 |
|
|
@SuppressWarnings("unchecked") T t = (T) r; |
613 |
|
|
d.completeValue(f.apply(t)); |
614 |
|
|
} |
615 |
|
|
} catch (Throwable ex) { |
616 |
|
|
d.completeThrowable(ex); |
617 |
|
|
} |
618 |
|
|
} |
619 |
dl |
1.113 |
dep = null; src = null; fn = null; |
620 |
|
|
return d.postFire(a, mode); |
621 |
dl |
1.7 |
} |
622 |
|
|
} |
623 |
|
|
|
624 |
jsr166 |
1.124 |
private <V> CompletableFuture<V> uniApplyStage( |
625 |
|
|
Executor e, Function<? super T,? extends V> f) { |
626 |
dl |
1.113 |
if (f == null) throw new NullPointerException(); |
627 |
jsr166 |
1.200 |
Object r; |
628 |
|
|
if ((r = result) != null) |
629 |
|
|
return uniApplyNow(r, e, f); |
630 |
dl |
1.143 |
CompletableFuture<V> d = newIncompleteFuture(); |
631 |
jsr166 |
1.200 |
unipush(new UniApply<T,V>(e, d, this, f)); |
632 |
|
|
return d; |
633 |
|
|
} |
634 |
|
|
|
635 |
|
|
private <V> CompletableFuture<V> uniApplyNow( |
636 |
|
|
Object r, Executor e, Function<? super T,? extends V> f) { |
637 |
|
|
Throwable x; |
638 |
|
|
CompletableFuture<V> d = newIncompleteFuture(); |
639 |
|
|
if (r instanceof AltResult) { |
640 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
641 |
jsr166 |
1.201 |
d.result = encodeThrowable(x, r); |
642 |
jsr166 |
1.200 |
return d; |
643 |
dl |
1.184 |
} |
644 |
jsr166 |
1.200 |
r = null; |
645 |
|
|
} |
646 |
|
|
try { |
647 |
|
|
if (e != null) { |
648 |
|
|
e.execute(new UniApply<T,V>(null, d, this, f)); |
649 |
|
|
} else { |
650 |
|
|
@SuppressWarnings("unchecked") T t = (T) r; |
651 |
jsr166 |
1.201 |
d.result = d.encodeValue(f.apply(t)); |
652 |
dl |
1.184 |
} |
653 |
jsr166 |
1.200 |
} catch (Throwable ex) { |
654 |
jsr166 |
1.201 |
d.result = encodeThrowable(ex); |
655 |
dl |
1.37 |
} |
656 |
dl |
1.113 |
return d; |
657 |
dl |
1.37 |
} |
658 |
|
|
|
659 |
dl |
1.113 |
@SuppressWarnings("serial") |
660 |
jsr166 |
1.124 |
static final class UniAccept<T> extends UniCompletion<T,Void> { |
661 |
dl |
1.104 |
Consumer<? super T> fn; |
662 |
dl |
1.113 |
UniAccept(Executor executor, CompletableFuture<Void> dep, |
663 |
jsr166 |
1.124 |
CompletableFuture<T> src, Consumer<? super T> fn) { |
664 |
dl |
1.113 |
super(executor, dep, src); this.fn = fn; |
665 |
|
|
} |
666 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
667 |
|
|
CompletableFuture<Void> d; CompletableFuture<T> a; |
668 |
jsr166 |
1.200 |
Object r; Throwable x; Consumer<? super T> f; |
669 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
670 |
|
|
|| (a = src) == null || (r = a.result) == null) |
671 |
dl |
1.113 |
return null; |
672 |
jsr166 |
1.200 |
tryComplete: if (d.result == null) { |
673 |
|
|
if (r instanceof AltResult) { |
674 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
675 |
|
|
d.completeThrowable(x, r); |
676 |
|
|
break tryComplete; |
677 |
|
|
} |
678 |
|
|
r = null; |
679 |
|
|
} |
680 |
|
|
try { |
681 |
|
|
if (mode <= 0 && !claim()) |
682 |
|
|
return null; |
683 |
|
|
else { |
684 |
|
|
@SuppressWarnings("unchecked") T t = (T) r; |
685 |
|
|
f.accept(t); |
686 |
|
|
d.completeNull(); |
687 |
|
|
} |
688 |
|
|
} catch (Throwable ex) { |
689 |
|
|
d.completeThrowable(ex); |
690 |
|
|
} |
691 |
|
|
} |
692 |
dl |
1.113 |
dep = null; src = null; fn = null; |
693 |
|
|
return d.postFire(a, mode); |
694 |
dl |
1.91 |
} |
695 |
|
|
} |
696 |
|
|
|
697 |
dl |
1.113 |
private CompletableFuture<Void> uniAcceptStage(Executor e, |
698 |
|
|
Consumer<? super T> f) { |
699 |
|
|
if (f == null) throw new NullPointerException(); |
700 |
jsr166 |
1.200 |
Object r; |
701 |
|
|
if ((r = result) != null) |
702 |
|
|
return uniAcceptNow(r, e, f); |
703 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
704 |
jsr166 |
1.200 |
unipush(new UniAccept<T>(e, d, this, f)); |
705 |
|
|
return d; |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
private CompletableFuture<Void> uniAcceptNow( |
709 |
|
|
Object r, Executor e, Consumer<? super T> f) { |
710 |
|
|
Throwable x; |
711 |
|
|
CompletableFuture<Void> d = newIncompleteFuture(); |
712 |
|
|
if (r instanceof AltResult) { |
713 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
714 |
jsr166 |
1.201 |
d.result = encodeThrowable(x, r); |
715 |
jsr166 |
1.200 |
return d; |
716 |
dl |
1.184 |
} |
717 |
jsr166 |
1.200 |
r = null; |
718 |
|
|
} |
719 |
|
|
try { |
720 |
|
|
if (e != null) { |
721 |
|
|
e.execute(new UniAccept<T>(null, d, this, f)); |
722 |
|
|
} else { |
723 |
|
|
@SuppressWarnings("unchecked") T t = (T) r; |
724 |
|
|
f.accept(t); |
725 |
jsr166 |
1.201 |
d.result = NIL; |
726 |
dl |
1.184 |
} |
727 |
jsr166 |
1.200 |
} catch (Throwable ex) { |
728 |
jsr166 |
1.201 |
d.result = encodeThrowable(ex); |
729 |
dl |
1.7 |
} |
730 |
dl |
1.113 |
return d; |
731 |
dl |
1.7 |
} |
732 |
|
|
|
733 |
dl |
1.113 |
@SuppressWarnings("serial") |
734 |
jsr166 |
1.124 |
static final class UniRun<T> extends UniCompletion<T,Void> { |
735 |
jsr166 |
1.105 |
Runnable fn; |
736 |
dl |
1.113 |
UniRun(Executor executor, CompletableFuture<Void> dep, |
737 |
jsr166 |
1.124 |
CompletableFuture<T> src, Runnable fn) { |
738 |
dl |
1.113 |
super(executor, dep, src); this.fn = fn; |
739 |
|
|
} |
740 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
741 |
|
|
CompletableFuture<Void> d; CompletableFuture<T> a; |
742 |
jsr166 |
1.200 |
Object r; Throwable x; Runnable f; |
743 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
744 |
|
|
|| (a = src) == null || (r = a.result) == null) |
745 |
dl |
1.113 |
return null; |
746 |
jsr166 |
1.200 |
if (d.result == null) { |
747 |
|
|
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
748 |
|
|
d.completeThrowable(x, r); |
749 |
|
|
else |
750 |
|
|
try { |
751 |
|
|
if (mode <= 0 && !claim()) |
752 |
|
|
return null; |
753 |
|
|
else { |
754 |
|
|
f.run(); |
755 |
|
|
d.completeNull(); |
756 |
|
|
} |
757 |
|
|
} catch (Throwable ex) { |
758 |
|
|
d.completeThrowable(ex); |
759 |
|
|
} |
760 |
|
|
} |
761 |
dl |
1.113 |
dep = null; src = null; fn = null; |
762 |
|
|
return d.postFire(a, mode); |
763 |
dl |
1.1 |
} |
764 |
|
|
} |
765 |
|
|
|
766 |
jsr166 |
1.200 |
private CompletableFuture<Void> uniRunStage(Executor e, Runnable f) { |
767 |
|
|
if (f == null) throw new NullPointerException(); |
768 |
|
|
Object r; |
769 |
|
|
if ((r = result) != null) |
770 |
|
|
return uniRunNow(r, e, f); |
771 |
|
|
CompletableFuture<Void> d = newIncompleteFuture(); |
772 |
|
|
unipush(new UniRun<T>(e, d, this, f)); |
773 |
|
|
return d; |
774 |
dl |
1.1 |
} |
775 |
|
|
|
776 |
jsr166 |
1.200 |
private CompletableFuture<Void> uniRunNow(Object r, Executor e, Runnable f) { |
777 |
|
|
Throwable x; |
778 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
779 |
jsr166 |
1.200 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) |
780 |
jsr166 |
1.201 |
d.result = encodeThrowable(x, r); |
781 |
jsr166 |
1.200 |
else |
782 |
|
|
try { |
783 |
|
|
if (e != null) { |
784 |
|
|
e.execute(new UniRun<T>(null, d, this, f)); |
785 |
|
|
} else { |
786 |
|
|
f.run(); |
787 |
jsr166 |
1.201 |
d.result = NIL; |
788 |
dl |
1.184 |
} |
789 |
jsr166 |
1.200 |
} catch (Throwable ex) { |
790 |
jsr166 |
1.201 |
d.result = encodeThrowable(ex); |
791 |
dl |
1.184 |
} |
792 |
dl |
1.113 |
return d; |
793 |
dl |
1.7 |
} |
794 |
|
|
|
795 |
dl |
1.113 |
@SuppressWarnings("serial") |
796 |
jsr166 |
1.124 |
static final class UniWhenComplete<T> extends UniCompletion<T,T> { |
797 |
dl |
1.113 |
BiConsumer<? super T, ? super Throwable> fn; |
798 |
|
|
UniWhenComplete(Executor executor, CompletableFuture<T> dep, |
799 |
jsr166 |
1.124 |
CompletableFuture<T> src, |
800 |
dl |
1.113 |
BiConsumer<? super T, ? super Throwable> fn) { |
801 |
|
|
super(executor, dep, src); this.fn = fn; |
802 |
|
|
} |
803 |
jsr166 |
1.124 |
final CompletableFuture<T> tryFire(int mode) { |
804 |
|
|
CompletableFuture<T> d; CompletableFuture<T> a; |
805 |
jsr166 |
1.201 |
Object r; BiConsumer<? super T, ? super Throwable> f; |
806 |
|
|
if ((d = dep) == null || (f = fn) == null |
807 |
|
|
|| (a = src) == null || (r = a.result) == null |
808 |
|
|
|| !d.uniWhenComplete(r, f, mode > 0 ? null : this)) |
809 |
dl |
1.113 |
return null; |
810 |
|
|
dep = null; src = null; fn = null; |
811 |
|
|
return d.postFire(a, mode); |
812 |
|
|
} |
813 |
|
|
} |
814 |
dl |
1.104 |
|
815 |
jsr166 |
1.201 |
final boolean uniWhenComplete(Object r, |
816 |
dl |
1.113 |
BiConsumer<? super T,? super Throwable> f, |
817 |
|
|
UniWhenComplete<T> c) { |
818 |
jsr166 |
1.201 |
T t; Throwable x = null; |
819 |
dl |
1.113 |
if (result == null) { |
820 |
dl |
1.104 |
try { |
821 |
jsr166 |
1.127 |
if (c != null && !c.claim()) |
822 |
|
|
return false; |
823 |
dl |
1.113 |
if (r instanceof AltResult) { |
824 |
|
|
x = ((AltResult)r).ex; |
825 |
jsr166 |
1.127 |
t = null; |
826 |
|
|
} else { |
827 |
|
|
@SuppressWarnings("unchecked") T tr = (T) r; |
828 |
|
|
t = tr; |
829 |
dl |
1.113 |
} |
830 |
jsr166 |
1.127 |
f.accept(t, x); |
831 |
|
|
if (x == null) { |
832 |
|
|
internalComplete(r); |
833 |
|
|
return true; |
834 |
jsr166 |
1.2 |
} |
835 |
dl |
1.104 |
} catch (Throwable ex) { |
836 |
jsr166 |
1.127 |
if (x == null) |
837 |
|
|
x = ex; |
838 |
jsr166 |
1.180 |
else if (x != ex) |
839 |
|
|
x.addSuppressed(ex); |
840 |
jsr166 |
1.2 |
} |
841 |
jsr166 |
1.128 |
completeThrowable(x, r); |
842 |
jsr166 |
1.2 |
} |
843 |
dl |
1.113 |
return true; |
844 |
dl |
1.1 |
} |
845 |
|
|
|
846 |
dl |
1.113 |
private CompletableFuture<T> uniWhenCompleteStage( |
847 |
|
|
Executor e, BiConsumer<? super T, ? super Throwable> f) { |
848 |
|
|
if (f == null) throw new NullPointerException(); |
849 |
dl |
1.143 |
CompletableFuture<T> d = newIncompleteFuture(); |
850 |
jsr166 |
1.201 |
Object r; |
851 |
|
|
if ((r = result) == null) |
852 |
|
|
unipush(new UniWhenComplete<T>(e, d, this, f)); |
853 |
|
|
else if (e == null) |
854 |
|
|
d.uniWhenComplete(r, f, null); |
855 |
|
|
else { |
856 |
|
|
try { |
857 |
|
|
e.execute(new UniWhenComplete<T>(null, d, this, f)); |
858 |
|
|
} catch (Throwable ex) { |
859 |
|
|
d.result = encodeThrowable(ex); |
860 |
dl |
1.184 |
} |
861 |
dl |
1.35 |
} |
862 |
dl |
1.113 |
return d; |
863 |
dl |
1.35 |
} |
864 |
|
|
|
865 |
dl |
1.113 |
@SuppressWarnings("serial") |
866 |
jsr166 |
1.124 |
static final class UniHandle<T,V> extends UniCompletion<T,V> { |
867 |
|
|
BiFunction<? super T, Throwable, ? extends V> fn; |
868 |
|
|
UniHandle(Executor executor, CompletableFuture<V> dep, |
869 |
|
|
CompletableFuture<T> src, |
870 |
|
|
BiFunction<? super T, Throwable, ? extends V> fn) { |
871 |
dl |
1.113 |
super(executor, dep, src); this.fn = fn; |
872 |
|
|
} |
873 |
jsr166 |
1.124 |
final CompletableFuture<V> tryFire(int mode) { |
874 |
|
|
CompletableFuture<V> d; CompletableFuture<T> a; |
875 |
jsr166 |
1.201 |
Object r; BiFunction<? super T, Throwable, ? extends V> f; |
876 |
|
|
if ((d = dep) == null || (f = fn) == null |
877 |
|
|
|| (a = src) == null || (r = a.result) == null |
878 |
|
|
|| !d.uniHandle(r, f, mode > 0 ? null : this)) |
879 |
dl |
1.113 |
return null; |
880 |
|
|
dep = null; src = null; fn = null; |
881 |
|
|
return d.postFire(a, mode); |
882 |
jsr166 |
1.2 |
} |
883 |
dl |
1.1 |
} |
884 |
|
|
|
885 |
jsr166 |
1.201 |
final <S> boolean uniHandle(Object r, |
886 |
dl |
1.113 |
BiFunction<? super S, Throwable, ? extends T> f, |
887 |
|
|
UniHandle<S,T> c) { |
888 |
jsr166 |
1.201 |
S s; Throwable x; |
889 |
dl |
1.113 |
if (result == null) { |
890 |
dl |
1.104 |
try { |
891 |
jsr166 |
1.127 |
if (c != null && !c.claim()) |
892 |
|
|
return false; |
893 |
dl |
1.113 |
if (r instanceof AltResult) { |
894 |
|
|
x = ((AltResult)r).ex; |
895 |
jsr166 |
1.127 |
s = null; |
896 |
|
|
} else { |
897 |
dl |
1.113 |
x = null; |
898 |
jsr166 |
1.127 |
@SuppressWarnings("unchecked") S ss = (S) r; |
899 |
|
|
s = ss; |
900 |
dl |
1.1 |
} |
901 |
jsr166 |
1.127 |
completeValue(f.apply(s, x)); |
902 |
dl |
1.104 |
} catch (Throwable ex) { |
903 |
jsr166 |
1.127 |
completeThrowable(ex); |
904 |
jsr166 |
1.2 |
} |
905 |
|
|
} |
906 |
dl |
1.113 |
return true; |
907 |
dl |
1.1 |
} |
908 |
|
|
|
909 |
jsr166 |
1.124 |
private <V> CompletableFuture<V> uniHandleStage( |
910 |
|
|
Executor e, BiFunction<? super T, Throwable, ? extends V> f) { |
911 |
dl |
1.113 |
if (f == null) throw new NullPointerException(); |
912 |
dl |
1.143 |
CompletableFuture<V> d = newIncompleteFuture(); |
913 |
jsr166 |
1.201 |
Object r; |
914 |
|
|
if ((r = result) == null) |
915 |
|
|
unipush(new UniHandle<T,V>(e, d, this, f)); |
916 |
|
|
else if (e == null) |
917 |
|
|
d.uniHandle(r, f, null); |
918 |
|
|
else { |
919 |
|
|
try { |
920 |
|
|
e.execute(new UniHandle<T,V>(null, d, this, f)); |
921 |
|
|
} catch (Throwable ex) { |
922 |
|
|
d.result = encodeThrowable(ex); |
923 |
dl |
1.184 |
} |
924 |
dl |
1.35 |
} |
925 |
dl |
1.104 |
return d; |
926 |
dl |
1.1 |
} |
927 |
|
|
|
928 |
dl |
1.113 |
@SuppressWarnings("serial") |
929 |
jsr166 |
1.124 |
static final class UniExceptionally<T> extends UniCompletion<T,T> { |
930 |
dl |
1.104 |
Function<? super Throwable, ? extends T> fn; |
931 |
jsr166 |
1.124 |
UniExceptionally(CompletableFuture<T> dep, CompletableFuture<T> src, |
932 |
dl |
1.113 |
Function<? super Throwable, ? extends T> fn) { |
933 |
dl |
1.104 |
super(null, dep, src); this.fn = fn; |
934 |
|
|
} |
935 |
jsr166 |
1.124 |
final CompletableFuture<T> tryFire(int mode) { // never ASYNC |
936 |
jsr166 |
1.128 |
// assert mode != ASYNC; |
937 |
jsr166 |
1.124 |
CompletableFuture<T> d; CompletableFuture<T> a; |
938 |
jsr166 |
1.201 |
Object r; Function<? super Throwable, ? extends T> f; |
939 |
|
|
if ((d = dep) == null || (f = fn) == null |
940 |
|
|
|| (a = src) == null || (r = a.result) == null |
941 |
|
|
|| !d.uniExceptionally(r, f, this)) |
942 |
dl |
1.113 |
return null; |
943 |
|
|
dep = null; src = null; fn = null; |
944 |
|
|
return d.postFire(a, mode); |
945 |
dl |
1.17 |
} |
946 |
|
|
} |
947 |
|
|
|
948 |
jsr166 |
1.201 |
final boolean uniExceptionally(Object r, |
949 |
dl |
1.113 |
Function<? super Throwable, ? extends T> f, |
950 |
|
|
UniExceptionally<T> c) { |
951 |
jsr166 |
1.201 |
Throwable x; |
952 |
dl |
1.113 |
if (result == null) { |
953 |
|
|
try { |
954 |
jsr166 |
1.127 |
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) { |
955 |
|
|
if (c != null && !c.claim()) |
956 |
|
|
return false; |
957 |
|
|
completeValue(f.apply(x)); |
958 |
|
|
} else |
959 |
|
|
internalComplete(r); |
960 |
dl |
1.113 |
} catch (Throwable ex) { |
961 |
jsr166 |
1.127 |
completeThrowable(ex); |
962 |
dl |
1.113 |
} |
963 |
|
|
} |
964 |
|
|
return true; |
965 |
dl |
1.75 |
} |
966 |
|
|
|
967 |
dl |
1.113 |
private CompletableFuture<T> uniExceptionallyStage( |
968 |
|
|
Function<Throwable, ? extends T> f) { |
969 |
|
|
if (f == null) throw new NullPointerException(); |
970 |
dl |
1.143 |
CompletableFuture<T> d = newIncompleteFuture(); |
971 |
jsr166 |
1.201 |
Object r; |
972 |
|
|
if ((r = result) == null) |
973 |
jsr166 |
1.188 |
unipush(new UniExceptionally<T>(d, this, f)); |
974 |
jsr166 |
1.201 |
else |
975 |
|
|
d.uniExceptionally(r, f, null); |
976 |
dl |
1.113 |
return d; |
977 |
dl |
1.17 |
} |
978 |
|
|
|
979 |
dl |
1.113 |
@SuppressWarnings("serial") |
980 |
jsr166 |
1.207 |
static final class UniRelay<U, T extends U> extends UniCompletion<T,U> { |
981 |
|
|
UniRelay(CompletableFuture<U> dep, CompletableFuture<T> src) { |
982 |
dl |
1.104 |
super(null, dep, src); |
983 |
|
|
} |
984 |
jsr166 |
1.207 |
final CompletableFuture<U> tryFire(int mode) { |
985 |
|
|
CompletableFuture<U> d; CompletableFuture<T> a; Object r; |
986 |
jsr166 |
1.201 |
if ((d = dep) == null |
987 |
|
|
|| (a = src) == null || (r = a.result) == null) |
988 |
dl |
1.113 |
return null; |
989 |
jsr166 |
1.201 |
if (d.result == null) |
990 |
|
|
d.completeRelay(r); |
991 |
dl |
1.113 |
src = null; dep = null; |
992 |
|
|
return d.postFire(a, mode); |
993 |
dl |
1.28 |
} |
994 |
|
|
} |
995 |
|
|
|
996 |
jsr166 |
1.207 |
private static <U, T extends U> CompletableFuture<U> uniCopyStage( |
997 |
|
|
CompletableFuture<T> src) { |
998 |
dl |
1.143 |
Object r; |
999 |
jsr166 |
1.207 |
CompletableFuture<U> d = src.newIncompleteFuture(); |
1000 |
|
|
if ((r = src.result) != null) |
1001 |
jsr166 |
1.201 |
d.result = encodeRelay(r); |
1002 |
|
|
else |
1003 |
jsr166 |
1.207 |
src.unipush(new UniRelay<U,T>(d, src)); |
1004 |
dl |
1.143 |
return d; |
1005 |
|
|
} |
1006 |
|
|
|
1007 |
|
|
private MinimalStage<T> uniAsMinimalStage() { |
1008 |
|
|
Object r; |
1009 |
|
|
if ((r = result) != null) |
1010 |
|
|
return new MinimalStage<T>(encodeRelay(r)); |
1011 |
|
|
MinimalStage<T> d = new MinimalStage<T>(); |
1012 |
jsr166 |
1.207 |
unipush(new UniRelay<T,T>(d, this)); |
1013 |
dl |
1.143 |
return d; |
1014 |
|
|
} |
1015 |
|
|
|
1016 |
dl |
1.113 |
@SuppressWarnings("serial") |
1017 |
jsr166 |
1.124 |
static final class UniCompose<T,V> extends UniCompletion<T,V> { |
1018 |
|
|
Function<? super T, ? extends CompletionStage<V>> fn; |
1019 |
|
|
UniCompose(Executor executor, CompletableFuture<V> dep, |
1020 |
|
|
CompletableFuture<T> src, |
1021 |
|
|
Function<? super T, ? extends CompletionStage<V>> fn) { |
1022 |
dl |
1.113 |
super(executor, dep, src); this.fn = fn; |
1023 |
|
|
} |
1024 |
jsr166 |
1.124 |
final CompletableFuture<V> tryFire(int mode) { |
1025 |
|
|
CompletableFuture<V> d; CompletableFuture<T> a; |
1026 |
jsr166 |
1.201 |
Function<? super T, ? extends CompletionStage<V>> f; |
1027 |
|
|
Object r; Throwable x; |
1028 |
|
|
if ((d = dep) == null || (f = fn) == null |
1029 |
|
|
|| (a = src) == null || (r = a.result) == null) |
1030 |
dl |
1.113 |
return null; |
1031 |
jsr166 |
1.201 |
tryComplete: if (d.result == null) { |
1032 |
|
|
if (r instanceof AltResult) { |
1033 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1034 |
|
|
d.completeThrowable(x, r); |
1035 |
|
|
break tryComplete; |
1036 |
|
|
} |
1037 |
|
|
r = null; |
1038 |
jsr166 |
1.128 |
} |
1039 |
jsr166 |
1.201 |
try { |
1040 |
|
|
if (mode <= 0 && !claim()) |
1041 |
|
|
return null; |
1042 |
|
|
@SuppressWarnings("unchecked") T t = (T) r; |
1043 |
|
|
CompletableFuture<V> g = f.apply(t).toCompletableFuture(); |
1044 |
|
|
if ((r = g.result) != null) |
1045 |
|
|
d.completeRelay(r); |
1046 |
|
|
else { |
1047 |
jsr166 |
1.207 |
g.unipush(new UniRelay<V,V>(d, g)); |
1048 |
jsr166 |
1.201 |
if (d.result == null) |
1049 |
|
|
return null; |
1050 |
|
|
} |
1051 |
|
|
} catch (Throwable ex) { |
1052 |
|
|
d.completeThrowable(ex); |
1053 |
dl |
1.88 |
} |
1054 |
|
|
} |
1055 |
jsr166 |
1.201 |
dep = null; src = null; fn = null; |
1056 |
|
|
return d.postFire(a, mode); |
1057 |
dl |
1.88 |
} |
1058 |
dl |
1.28 |
} |
1059 |
|
|
|
1060 |
jsr166 |
1.124 |
private <V> CompletableFuture<V> uniComposeStage( |
1061 |
|
|
Executor e, Function<? super T, ? extends CompletionStage<V>> f) { |
1062 |
dl |
1.113 |
if (f == null) throw new NullPointerException(); |
1063 |
jsr166 |
1.201 |
CompletableFuture<V> d = newIncompleteFuture(); |
1064 |
dl |
1.143 |
Object r, s; Throwable x; |
1065 |
jsr166 |
1.201 |
if ((r = result) == null) |
1066 |
|
|
unipush(new UniCompose<T,V>(e, d, this, f)); |
1067 |
|
|
else if (e == null) { |
1068 |
jsr166 |
1.128 |
if (r instanceof AltResult) { |
1069 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1070 |
dl |
1.143 |
d.result = encodeThrowable(x, r); |
1071 |
|
|
return d; |
1072 |
jsr166 |
1.128 |
} |
1073 |
|
|
r = null; |
1074 |
|
|
} |
1075 |
|
|
try { |
1076 |
jsr166 |
1.127 |
@SuppressWarnings("unchecked") T t = (T) r; |
1077 |
dl |
1.140 |
CompletableFuture<V> g = f.apply(t).toCompletableFuture(); |
1078 |
dl |
1.143 |
if ((s = g.result) != null) |
1079 |
jsr166 |
1.201 |
d.result = encodeRelay(s); |
1080 |
dl |
1.143 |
else { |
1081 |
jsr166 |
1.207 |
g.unipush(new UniRelay<V,V>(d, g)); |
1082 |
dl |
1.143 |
} |
1083 |
dl |
1.113 |
} catch (Throwable ex) { |
1084 |
dl |
1.143 |
d.result = encodeThrowable(ex); |
1085 |
dl |
1.104 |
} |
1086 |
|
|
} |
1087 |
jsr166 |
1.201 |
else |
1088 |
dl |
1.184 |
try { |
1089 |
|
|
e.execute(new UniCompose<T,V>(null, d, this, f)); |
1090 |
|
|
} catch (Throwable ex) { |
1091 |
jsr166 |
1.201 |
d.result = encodeThrowable(ex); |
1092 |
dl |
1.184 |
} |
1093 |
dl |
1.113 |
return d; |
1094 |
dl |
1.28 |
} |
1095 |
|
|
|
1096 |
dl |
1.113 |
/* ------------- Two-input Completions -------------- */ |
1097 |
dl |
1.104 |
|
1098 |
dl |
1.113 |
/** A Completion for an action with two sources */ |
1099 |
|
|
@SuppressWarnings("serial") |
1100 |
jsr166 |
1.124 |
abstract static class BiCompletion<T,U,V> extends UniCompletion<T,V> { |
1101 |
|
|
CompletableFuture<U> snd; // second source for action |
1102 |
|
|
BiCompletion(Executor executor, CompletableFuture<V> dep, |
1103 |
|
|
CompletableFuture<T> src, CompletableFuture<U> snd) { |
1104 |
dl |
1.113 |
super(executor, dep, src); this.snd = snd; |
1105 |
dl |
1.104 |
} |
1106 |
|
|
} |
1107 |
|
|
|
1108 |
dl |
1.113 |
/** A Completion delegating to a BiCompletion */ |
1109 |
|
|
@SuppressWarnings("serial") |
1110 |
dl |
1.110 |
static final class CoCompletion extends Completion { |
1111 |
jsr166 |
1.124 |
BiCompletion<?,?,?> base; |
1112 |
|
|
CoCompletion(BiCompletion<?,?,?> base) { this.base = base; } |
1113 |
dl |
1.113 |
final CompletableFuture<?> tryFire(int mode) { |
1114 |
jsr166 |
1.124 |
BiCompletion<?,?,?> c; CompletableFuture<?> d; |
1115 |
dl |
1.113 |
if ((c = base) == null || (d = c.tryFire(mode)) == null) |
1116 |
dl |
1.110 |
return null; |
1117 |
dl |
1.113 |
base = null; // detach |
1118 |
dl |
1.110 |
return d; |
1119 |
dl |
1.88 |
} |
1120 |
dl |
1.113 |
final boolean isLive() { |
1121 |
jsr166 |
1.124 |
BiCompletion<?,?,?> c; |
1122 |
jsr166 |
1.197 |
return (c = base) != null |
1123 |
|
|
// && c.isLive() |
1124 |
|
|
&& c.dep != null; |
1125 |
dl |
1.113 |
} |
1126 |
dl |
1.88 |
} |
1127 |
|
|
|
1128 |
jsr166 |
1.198 |
/** |
1129 |
|
|
* Pushes completion to this and b unless both done. |
1130 |
|
|
* Caller should first check that either result or b.result is null. |
1131 |
|
|
*/ |
1132 |
jsr166 |
1.124 |
final void bipush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
1133 |
dl |
1.113 |
if (c != null) { |
1134 |
jsr166 |
1.198 |
while (result == null) { |
1135 |
|
|
if (tryPushStack(c)) { |
1136 |
|
|
if (b.result == null) |
1137 |
|
|
b.unipush(new CoCompletion(c)); |
1138 |
|
|
else if (result != null) |
1139 |
|
|
c.tryFire(SYNC); |
1140 |
|
|
return; |
1141 |
|
|
} |
1142 |
dl |
1.88 |
} |
1143 |
jsr166 |
1.198 |
b.unipush(c); |
1144 |
dl |
1.88 |
} |
1145 |
dl |
1.104 |
} |
1146 |
|
|
|
1147 |
dl |
1.113 |
/** Post-processing after successful BiCompletion tryFire. */ |
1148 |
|
|
final CompletableFuture<T> postFire(CompletableFuture<?> a, |
1149 |
|
|
CompletableFuture<?> b, int mode) { |
1150 |
|
|
if (b != null && b.stack != null) { // clean second source |
1151 |
dl |
1.185 |
Object r; |
1152 |
|
|
if ((r = b.result) == null) |
1153 |
dl |
1.113 |
b.cleanStack(); |
1154 |
dl |
1.185 |
if (mode >= 0 && (r != null || b.result != null)) |
1155 |
dl |
1.113 |
b.postComplete(); |
1156 |
dl |
1.88 |
} |
1157 |
dl |
1.113 |
return postFire(a, mode); |
1158 |
dl |
1.88 |
} |
1159 |
|
|
|
1160 |
dl |
1.113 |
@SuppressWarnings("serial") |
1161 |
jsr166 |
1.124 |
static final class BiApply<T,U,V> extends BiCompletion<T,U,V> { |
1162 |
dl |
1.113 |
BiFunction<? super T,? super U,? extends V> fn; |
1163 |
|
|
BiApply(Executor executor, CompletableFuture<V> dep, |
1164 |
jsr166 |
1.124 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1165 |
dl |
1.113 |
BiFunction<? super T,? super U,? extends V> fn) { |
1166 |
|
|
super(executor, dep, src, snd); this.fn = fn; |
1167 |
|
|
} |
1168 |
jsr166 |
1.124 |
final CompletableFuture<V> tryFire(int mode) { |
1169 |
|
|
CompletableFuture<V> d; |
1170 |
|
|
CompletableFuture<T> a; |
1171 |
|
|
CompletableFuture<U> b; |
1172 |
jsr166 |
1.201 |
Object r, s; BiFunction<? super T,? super U,? extends V> f; |
1173 |
|
|
if ((d = dep) == null || (f = fn) == null |
1174 |
|
|
|| (a = src) == null || (r = a.result) == null |
1175 |
|
|
|| (b = snd) == null || (s = b.result) == null |
1176 |
|
|
|| !d.biApply(r, s, f, mode > 0 ? null : this)) |
1177 |
dl |
1.113 |
return null; |
1178 |
jsr166 |
1.124 |
dep = null; src = null; snd = null; fn = null; |
1179 |
dl |
1.113 |
return d.postFire(a, b, mode); |
1180 |
dl |
1.104 |
} |
1181 |
|
|
} |
1182 |
|
|
|
1183 |
jsr166 |
1.201 |
final <R,S> boolean biApply(Object r, Object s, |
1184 |
dl |
1.113 |
BiFunction<? super R,? super S,? extends T> f, |
1185 |
|
|
BiApply<R,S,T> c) { |
1186 |
jsr166 |
1.201 |
Throwable x; |
1187 |
jsr166 |
1.128 |
tryComplete: if (result == null) { |
1188 |
|
|
if (r instanceof AltResult) { |
1189 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1190 |
|
|
completeThrowable(x, r); |
1191 |
|
|
break tryComplete; |
1192 |
|
|
} |
1193 |
|
|
r = null; |
1194 |
|
|
} |
1195 |
|
|
if (s instanceof AltResult) { |
1196 |
|
|
if ((x = ((AltResult)s).ex) != null) { |
1197 |
|
|
completeThrowable(x, s); |
1198 |
|
|
break tryComplete; |
1199 |
|
|
} |
1200 |
|
|
s = null; |
1201 |
|
|
} |
1202 |
|
|
try { |
1203 |
jsr166 |
1.127 |
if (c != null && !c.claim()) |
1204 |
dl |
1.113 |
return false; |
1205 |
jsr166 |
1.127 |
@SuppressWarnings("unchecked") R rr = (R) r; |
1206 |
|
|
@SuppressWarnings("unchecked") S ss = (S) s; |
1207 |
|
|
completeValue(f.apply(rr, ss)); |
1208 |
dl |
1.113 |
} catch (Throwable ex) { |
1209 |
jsr166 |
1.128 |
completeThrowable(ex); |
1210 |
dl |
1.88 |
} |
1211 |
|
|
} |
1212 |
dl |
1.113 |
return true; |
1213 |
dl |
1.104 |
} |
1214 |
|
|
|
1215 |
dl |
1.113 |
private <U,V> CompletableFuture<V> biApplyStage( |
1216 |
jsr166 |
1.124 |
Executor e, CompletionStage<U> o, |
1217 |
dl |
1.113 |
BiFunction<? super T,? super U,? extends V> f) { |
1218 |
jsr166 |
1.201 |
CompletableFuture<U> b; Object r, s; |
1219 |
dl |
1.113 |
if (f == null || (b = o.toCompletableFuture()) == null) |
1220 |
|
|
throw new NullPointerException(); |
1221 |
dl |
1.143 |
CompletableFuture<V> d = newIncompleteFuture(); |
1222 |
jsr166 |
1.201 |
if ((r = result) == null || (s = b.result) == null) |
1223 |
|
|
bipush(b, new BiApply<T,U,V>(e, d, this, b, f)); |
1224 |
|
|
else if (e == null) |
1225 |
|
|
d.biApply(r, s, f, null); |
1226 |
|
|
else |
1227 |
|
|
try { |
1228 |
|
|
e.execute(new BiApply<T,U,V>(null, d, this, b, f)); |
1229 |
|
|
} catch (Throwable ex) { |
1230 |
|
|
d.result = encodeThrowable(ex); |
1231 |
dl |
1.184 |
} |
1232 |
dl |
1.104 |
return d; |
1233 |
|
|
} |
1234 |
|
|
|
1235 |
dl |
1.113 |
@SuppressWarnings("serial") |
1236 |
jsr166 |
1.124 |
static final class BiAccept<T,U> extends BiCompletion<T,U,Void> { |
1237 |
dl |
1.113 |
BiConsumer<? super T,? super U> fn; |
1238 |
|
|
BiAccept(Executor executor, CompletableFuture<Void> dep, |
1239 |
jsr166 |
1.124 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1240 |
dl |
1.113 |
BiConsumer<? super T,? super U> fn) { |
1241 |
|
|
super(executor, dep, src, snd); this.fn = fn; |
1242 |
|
|
} |
1243 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
1244 |
|
|
CompletableFuture<Void> d; |
1245 |
|
|
CompletableFuture<T> a; |
1246 |
|
|
CompletableFuture<U> b; |
1247 |
jsr166 |
1.201 |
Object r, s; BiConsumer<? super T,? super U> f; |
1248 |
|
|
if ((d = dep) == null || (f = fn) == null |
1249 |
|
|
|| (a = src) == null || (r = a.result) == null |
1250 |
|
|
|| (b = snd) == null || (s = b.result) == null |
1251 |
|
|
|| !d.biAccept(r, s, f, mode > 0 ? null : this)) |
1252 |
dl |
1.113 |
return null; |
1253 |
jsr166 |
1.124 |
dep = null; src = null; snd = null; fn = null; |
1254 |
dl |
1.113 |
return d.postFire(a, b, mode); |
1255 |
|
|
} |
1256 |
|
|
} |
1257 |
dl |
1.104 |
|
1258 |
jsr166 |
1.201 |
final <R,S> boolean biAccept(Object r, Object s, |
1259 |
dl |
1.113 |
BiConsumer<? super R,? super S> f, |
1260 |
|
|
BiAccept<R,S> c) { |
1261 |
jsr166 |
1.201 |
Throwable x; |
1262 |
jsr166 |
1.128 |
tryComplete: if (result == null) { |
1263 |
|
|
if (r instanceof AltResult) { |
1264 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1265 |
|
|
completeThrowable(x, r); |
1266 |
|
|
break tryComplete; |
1267 |
|
|
} |
1268 |
|
|
r = null; |
1269 |
|
|
} |
1270 |
|
|
if (s instanceof AltResult) { |
1271 |
|
|
if ((x = ((AltResult)s).ex) != null) { |
1272 |
|
|
completeThrowable(x, s); |
1273 |
|
|
break tryComplete; |
1274 |
|
|
} |
1275 |
|
|
s = null; |
1276 |
|
|
} |
1277 |
|
|
try { |
1278 |
jsr166 |
1.127 |
if (c != null && !c.claim()) |
1279 |
|
|
return false; |
1280 |
|
|
@SuppressWarnings("unchecked") R rr = (R) r; |
1281 |
|
|
@SuppressWarnings("unchecked") S ss = (S) s; |
1282 |
|
|
f.accept(rr, ss); |
1283 |
jsr166 |
1.128 |
completeNull(); |
1284 |
dl |
1.113 |
} catch (Throwable ex) { |
1285 |
jsr166 |
1.128 |
completeThrowable(ex); |
1286 |
dl |
1.88 |
} |
1287 |
dl |
1.104 |
} |
1288 |
dl |
1.113 |
return true; |
1289 |
dl |
1.104 |
} |
1290 |
|
|
|
1291 |
dl |
1.113 |
private <U> CompletableFuture<Void> biAcceptStage( |
1292 |
jsr166 |
1.124 |
Executor e, CompletionStage<U> o, |
1293 |
dl |
1.113 |
BiConsumer<? super T,? super U> f) { |
1294 |
jsr166 |
1.201 |
CompletableFuture<U> b; Object r, s; |
1295 |
dl |
1.113 |
if (f == null || (b = o.toCompletableFuture()) == null) |
1296 |
|
|
throw new NullPointerException(); |
1297 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
1298 |
jsr166 |
1.201 |
if ((r = result) == null || (s = b.result) == null) |
1299 |
|
|
bipush(b, new BiAccept<T,U>(e, d, this, b, f)); |
1300 |
|
|
else if (e == null) |
1301 |
|
|
d.biAccept(r, s, f, null); |
1302 |
|
|
else |
1303 |
|
|
try { |
1304 |
|
|
e.execute(new BiAccept<T,U>(null, d, this, b, f)); |
1305 |
|
|
} catch (Throwable ex) { |
1306 |
|
|
d.result = encodeThrowable(ex); |
1307 |
dl |
1.184 |
} |
1308 |
dl |
1.113 |
return d; |
1309 |
dl |
1.104 |
} |
1310 |
|
|
|
1311 |
dl |
1.113 |
@SuppressWarnings("serial") |
1312 |
jsr166 |
1.124 |
static final class BiRun<T,U> extends BiCompletion<T,U,Void> { |
1313 |
dl |
1.113 |
Runnable fn; |
1314 |
|
|
BiRun(Executor executor, CompletableFuture<Void> dep, |
1315 |
jsr166 |
1.203 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1316 |
dl |
1.113 |
Runnable fn) { |
1317 |
|
|
super(executor, dep, src, snd); this.fn = fn; |
1318 |
|
|
} |
1319 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
1320 |
|
|
CompletableFuture<Void> d; |
1321 |
|
|
CompletableFuture<T> a; |
1322 |
|
|
CompletableFuture<U> b; |
1323 |
jsr166 |
1.201 |
Object r, s; Runnable f; |
1324 |
|
|
if ((d = dep) == null || (f = fn) == null |
1325 |
|
|
|| (a = src) == null || (r = a.result) == null |
1326 |
|
|
|| (b = snd) == null || (s = b.result) == null |
1327 |
|
|
|| !d.biRun(r, s, f, mode > 0 ? null : this)) |
1328 |
dl |
1.113 |
return null; |
1329 |
jsr166 |
1.124 |
dep = null; src = null; snd = null; fn = null; |
1330 |
dl |
1.113 |
return d.postFire(a, b, mode); |
1331 |
dl |
1.88 |
} |
1332 |
|
|
} |
1333 |
|
|
|
1334 |
jsr166 |
1.201 |
final boolean biRun(Object r, Object s, Runnable f, BiRun<?,?> c) { |
1335 |
|
|
Throwable x; Object z; |
1336 |
dl |
1.113 |
if (result == null) { |
1337 |
jsr166 |
1.201 |
if ((r instanceof AltResult |
1338 |
|
|
&& (x = ((AltResult)(z = r)).ex) != null) || |
1339 |
|
|
(s instanceof AltResult |
1340 |
|
|
&& (x = ((AltResult)(z = s)).ex) != null)) |
1341 |
|
|
completeThrowable(x, z); |
1342 |
jsr166 |
1.128 |
else |
1343 |
|
|
try { |
1344 |
|
|
if (c != null && !c.claim()) |
1345 |
|
|
return false; |
1346 |
|
|
f.run(); |
1347 |
|
|
completeNull(); |
1348 |
|
|
} catch (Throwable ex) { |
1349 |
|
|
completeThrowable(ex); |
1350 |
|
|
} |
1351 |
dl |
1.88 |
} |
1352 |
dl |
1.113 |
return true; |
1353 |
dl |
1.104 |
} |
1354 |
|
|
|
1355 |
dl |
1.113 |
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o, |
1356 |
|
|
Runnable f) { |
1357 |
jsr166 |
1.201 |
CompletableFuture<?> b; Object r, s; |
1358 |
dl |
1.113 |
if (f == null || (b = o.toCompletableFuture()) == null) |
1359 |
|
|
throw new NullPointerException(); |
1360 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
1361 |
jsr166 |
1.201 |
if ((r = result) == null || (s = b.result) == null) |
1362 |
|
|
bipush(b, new BiRun<>(e, d, this, b, f)); |
1363 |
|
|
else if (e == null) |
1364 |
|
|
d.biRun(r, s, f, null); |
1365 |
|
|
else |
1366 |
|
|
try { |
1367 |
|
|
e.execute(new BiRun<>(null, d, this, b, f)); |
1368 |
|
|
} catch (Throwable ex) { |
1369 |
|
|
d.result = encodeThrowable(ex); |
1370 |
dl |
1.184 |
} |
1371 |
dl |
1.104 |
return d; |
1372 |
|
|
} |
1373 |
|
|
|
1374 |
dl |
1.113 |
@SuppressWarnings("serial") |
1375 |
jsr166 |
1.124 |
static final class BiRelay<T,U> extends BiCompletion<T,U,Void> { // for And |
1376 |
|
|
BiRelay(CompletableFuture<Void> dep, |
1377 |
jsr166 |
1.203 |
CompletableFuture<T> src, CompletableFuture<U> snd) { |
1378 |
dl |
1.113 |
super(null, dep, src, snd); |
1379 |
|
|
} |
1380 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
1381 |
|
|
CompletableFuture<Void> d; |
1382 |
|
|
CompletableFuture<T> a; |
1383 |
|
|
CompletableFuture<U> b; |
1384 |
jsr166 |
1.201 |
Object r, s, z; Throwable x; |
1385 |
|
|
if ((d = dep) == null |
1386 |
|
|
|| (a = src) == null || (r = a.result) == null |
1387 |
|
|
|| (b = snd) == null || (s = b.result) == null) |
1388 |
dl |
1.113 |
return null; |
1389 |
jsr166 |
1.201 |
if (d.result == null) { |
1390 |
|
|
if ((r instanceof AltResult |
1391 |
|
|
&& (x = ((AltResult)(z = r)).ex) != null) || |
1392 |
|
|
(s instanceof AltResult |
1393 |
|
|
&& (x = ((AltResult)(z = s)).ex) != null)) |
1394 |
|
|
d.completeThrowable(x, z); |
1395 |
|
|
else |
1396 |
|
|
d.completeNull(); |
1397 |
|
|
} |
1398 |
jsr166 |
1.124 |
src = null; snd = null; dep = null; |
1399 |
dl |
1.113 |
return d.postFire(a, b, mode); |
1400 |
|
|
} |
1401 |
|
|
} |
1402 |
dl |
1.104 |
|
1403 |
jsr166 |
1.117 |
/** Recursively constructs a tree of completions. */ |
1404 |
dl |
1.113 |
static CompletableFuture<Void> andTree(CompletableFuture<?>[] cfs, |
1405 |
|
|
int lo, int hi) { |
1406 |
dl |
1.104 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1407 |
|
|
if (lo > hi) // empty |
1408 |
|
|
d.result = NIL; |
1409 |
dl |
1.101 |
else { |
1410 |
jsr166 |
1.201 |
CompletableFuture<?> a, b; Object r, s, z; Throwable x; |
1411 |
dl |
1.104 |
int mid = (lo + hi) >>> 1; |
1412 |
dl |
1.113 |
if ((a = (lo == mid ? cfs[lo] : |
1413 |
jsr166 |
1.122 |
andTree(cfs, lo, mid))) == null || |
1414 |
dl |
1.113 |
(b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] : |
1415 |
jsr166 |
1.172 |
andTree(cfs, mid+1, hi))) == null) |
1416 |
dl |
1.113 |
throw new NullPointerException(); |
1417 |
jsr166 |
1.201 |
if ((r = a.result) == null || (s = b.result) == null) |
1418 |
jsr166 |
1.198 |
a.bipush(b, new BiRelay<>(d, a, b)); |
1419 |
jsr166 |
1.201 |
else if ((r instanceof AltResult |
1420 |
|
|
&& (x = ((AltResult)(z = r)).ex) != null) || |
1421 |
|
|
(s instanceof AltResult |
1422 |
|
|
&& (x = ((AltResult)(z = s)).ex) != null)) |
1423 |
|
|
d.result = encodeThrowable(x, z); |
1424 |
|
|
else |
1425 |
|
|
d.result = NIL; |
1426 |
dl |
1.88 |
} |
1427 |
dl |
1.104 |
return d; |
1428 |
dl |
1.88 |
} |
1429 |
|
|
|
1430 |
dl |
1.113 |
/* ------------- Projected (Ored) BiCompletions -------------- */ |
1431 |
dl |
1.104 |
|
1432 |
jsr166 |
1.198 |
/** |
1433 |
|
|
* Pushes completion to this and b unless either done. |
1434 |
|
|
* Caller should first check that result and b.result are both null. |
1435 |
|
|
*/ |
1436 |
jsr166 |
1.124 |
final void orpush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
1437 |
dl |
1.113 |
if (c != null) { |
1438 |
jsr166 |
1.198 |
while (!tryPushStack(c)) { |
1439 |
|
|
if (result != null) { |
1440 |
|
|
NEXT.set(c, null); |
1441 |
dl |
1.104 |
break; |
1442 |
dl |
1.88 |
} |
1443 |
|
|
} |
1444 |
jsr166 |
1.198 |
if (result != null) |
1445 |
|
|
c.tryFire(SYNC); |
1446 |
|
|
else |
1447 |
|
|
b.unipush(new CoCompletion(c)); |
1448 |
dl |
1.88 |
} |
1449 |
dl |
1.104 |
} |
1450 |
|
|
|
1451 |
dl |
1.113 |
@SuppressWarnings("serial") |
1452 |
jsr166 |
1.124 |
static final class OrApply<T,U extends T,V> extends BiCompletion<T,U,V> { |
1453 |
|
|
Function<? super T,? extends V> fn; |
1454 |
|
|
OrApply(Executor executor, CompletableFuture<V> dep, |
1455 |
jsr166 |
1.203 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1456 |
jsr166 |
1.124 |
Function<? super T,? extends V> fn) { |
1457 |
dl |
1.113 |
super(executor, dep, src, snd); this.fn = fn; |
1458 |
|
|
} |
1459 |
jsr166 |
1.124 |
final CompletableFuture<V> tryFire(int mode) { |
1460 |
|
|
CompletableFuture<V> d; |
1461 |
|
|
CompletableFuture<T> a; |
1462 |
|
|
CompletableFuture<U> b; |
1463 |
jsr166 |
1.200 |
Object r; Throwable x; Function<? super T,? extends V> f; |
1464 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
1465 |
|
|
|| (a = src) == null || (b = snd) == null |
1466 |
|
|
|| ((r = a.result) == null && (r = b.result) == null)) |
1467 |
dl |
1.113 |
return null; |
1468 |
jsr166 |
1.200 |
tryComplete: if (d.result == null) { |
1469 |
|
|
try { |
1470 |
|
|
if (mode <= 0 && !claim()) |
1471 |
|
|
return null; |
1472 |
|
|
if (r instanceof AltResult) { |
1473 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1474 |
|
|
d.completeThrowable(x, r); |
1475 |
|
|
break tryComplete; |
1476 |
|
|
} |
1477 |
|
|
r = null; |
1478 |
jsr166 |
1.128 |
} |
1479 |
jsr166 |
1.200 |
@SuppressWarnings("unchecked") T t = (T) r; |
1480 |
|
|
d.completeValue(f.apply(t)); |
1481 |
|
|
} catch (Throwable ex) { |
1482 |
|
|
d.completeThrowable(ex); |
1483 |
jsr166 |
1.128 |
} |
1484 |
dl |
1.88 |
} |
1485 |
jsr166 |
1.200 |
dep = null; src = null; snd = null; fn = null; |
1486 |
|
|
return d.postFire(a, b, mode); |
1487 |
dl |
1.88 |
} |
1488 |
|
|
} |
1489 |
|
|
|
1490 |
jsr166 |
1.124 |
private <U extends T,V> CompletableFuture<V> orApplyStage( |
1491 |
jsr166 |
1.200 |
Executor e, CompletionStage<U> o, Function<? super T, ? extends V> f) { |
1492 |
jsr166 |
1.124 |
CompletableFuture<U> b; |
1493 |
dl |
1.113 |
if (f == null || (b = o.toCompletableFuture()) == null) |
1494 |
|
|
throw new NullPointerException(); |
1495 |
jsr166 |
1.200 |
|
1496 |
|
|
Object r; CompletableFuture<? extends T> z; |
1497 |
|
|
if ((r = (z = this).result) != null || |
1498 |
|
|
(r = (z = b).result) != null) |
1499 |
|
|
return z.uniApplyNow(r, e, f); |
1500 |
|
|
|
1501 |
dl |
1.143 |
CompletableFuture<V> d = newIncompleteFuture(); |
1502 |
jsr166 |
1.200 |
orpush(b, new OrApply<T,U,V>(e, d, this, b, f)); |
1503 |
dl |
1.113 |
return d; |
1504 |
|
|
} |
1505 |
|
|
|
1506 |
|
|
@SuppressWarnings("serial") |
1507 |
jsr166 |
1.124 |
static final class OrAccept<T,U extends T> extends BiCompletion<T,U,Void> { |
1508 |
dl |
1.113 |
Consumer<? super T> fn; |
1509 |
|
|
OrAccept(Executor executor, CompletableFuture<Void> dep, |
1510 |
jsr166 |
1.203 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1511 |
dl |
1.113 |
Consumer<? super T> fn) { |
1512 |
|
|
super(executor, dep, src, snd); this.fn = fn; |
1513 |
|
|
} |
1514 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
1515 |
|
|
CompletableFuture<Void> d; |
1516 |
|
|
CompletableFuture<T> a; |
1517 |
|
|
CompletableFuture<U> b; |
1518 |
jsr166 |
1.200 |
Object r; Throwable x; Consumer<? super T> f; |
1519 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
1520 |
|
|
|| (a = src) == null || (b = snd) == null |
1521 |
|
|
|| ((r = a.result) == null && (r = b.result) == null)) |
1522 |
dl |
1.113 |
return null; |
1523 |
jsr166 |
1.200 |
tryComplete: if (d.result == null) { |
1524 |
|
|
try { |
1525 |
|
|
if (mode <= 0 && !claim()) |
1526 |
|
|
return null; |
1527 |
|
|
if (r instanceof AltResult) { |
1528 |
|
|
if ((x = ((AltResult)r).ex) != null) { |
1529 |
|
|
d.completeThrowable(x, r); |
1530 |
|
|
break tryComplete; |
1531 |
|
|
} |
1532 |
|
|
r = null; |
1533 |
jsr166 |
1.128 |
} |
1534 |
jsr166 |
1.200 |
@SuppressWarnings("unchecked") T t = (T) r; |
1535 |
|
|
f.accept(t); |
1536 |
|
|
d.completeNull(); |
1537 |
|
|
} catch (Throwable ex) { |
1538 |
|
|
d.completeThrowable(ex); |
1539 |
jsr166 |
1.128 |
} |
1540 |
dl |
1.113 |
} |
1541 |
jsr166 |
1.200 |
dep = null; src = null; snd = null; fn = null; |
1542 |
|
|
return d.postFire(a, b, mode); |
1543 |
dl |
1.113 |
} |
1544 |
|
|
} |
1545 |
|
|
|
1546 |
jsr166 |
1.124 |
private <U extends T> CompletableFuture<Void> orAcceptStage( |
1547 |
|
|
Executor e, CompletionStage<U> o, Consumer<? super T> f) { |
1548 |
|
|
CompletableFuture<U> b; |
1549 |
dl |
1.113 |
if (f == null || (b = o.toCompletableFuture()) == null) |
1550 |
|
|
throw new NullPointerException(); |
1551 |
jsr166 |
1.200 |
|
1552 |
|
|
Object r; CompletableFuture<? extends T> z; |
1553 |
|
|
if ((r = (z = this).result) != null || |
1554 |
|
|
(r = (z = b).result) != null) |
1555 |
|
|
return z.uniAcceptNow(r, e, f); |
1556 |
|
|
|
1557 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
1558 |
jsr166 |
1.200 |
orpush(b, new OrAccept<T,U>(e, d, this, b, f)); |
1559 |
dl |
1.104 |
return d; |
1560 |
|
|
} |
1561 |
|
|
|
1562 |
dl |
1.113 |
@SuppressWarnings("serial") |
1563 |
jsr166 |
1.124 |
static final class OrRun<T,U> extends BiCompletion<T,U,Void> { |
1564 |
dl |
1.113 |
Runnable fn; |
1565 |
|
|
OrRun(Executor executor, CompletableFuture<Void> dep, |
1566 |
jsr166 |
1.203 |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1567 |
jsr166 |
1.124 |
Runnable fn) { |
1568 |
dl |
1.113 |
super(executor, dep, src, snd); this.fn = fn; |
1569 |
|
|
} |
1570 |
jsr166 |
1.124 |
final CompletableFuture<Void> tryFire(int mode) { |
1571 |
|
|
CompletableFuture<Void> d; |
1572 |
|
|
CompletableFuture<T> a; |
1573 |
|
|
CompletableFuture<U> b; |
1574 |
jsr166 |
1.200 |
Object r; Throwable x; Runnable f; |
1575 |
jsr166 |
1.201 |
if ((d = dep) == null || (f = fn) == null |
1576 |
|
|
|| (a = src) == null || (b = snd) == null |
1577 |
|
|
|| ((r = a.result) == null && (r = b.result) == null)) |
1578 |
dl |
1.113 |
return null; |
1579 |
jsr166 |
1.200 |
if (d.result == null) { |
1580 |
|
|
try { |
1581 |
|
|
if (mode <= 0 && !claim()) |
1582 |
|
|
return null; |
1583 |
|
|
else if (r instanceof AltResult |
1584 |
|
|
&& (x = ((AltResult)r).ex) != null) |
1585 |
|
|
d.completeThrowable(x, r); |
1586 |
|
|
else { |
1587 |
|
|
f.run(); |
1588 |
|
|
d.completeNull(); |
1589 |
|
|
} |
1590 |
|
|
} catch (Throwable ex) { |
1591 |
|
|
d.completeThrowable(ex); |
1592 |
|
|
} |
1593 |
|
|
} |
1594 |
jsr166 |
1.124 |
dep = null; src = null; snd = null; fn = null; |
1595 |
dl |
1.113 |
return d.postFire(a, b, mode); |
1596 |
|
|
} |
1597 |
|
|
} |
1598 |
dl |
1.104 |
|
1599 |
dl |
1.113 |
private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o, |
1600 |
|
|
Runnable f) { |
1601 |
|
|
CompletableFuture<?> b; |
1602 |
|
|
if (f == null || (b = o.toCompletableFuture()) == null) |
1603 |
|
|
throw new NullPointerException(); |
1604 |
jsr166 |
1.200 |
|
1605 |
|
|
Object r; CompletableFuture<?> z; |
1606 |
|
|
if ((r = (z = this).result) != null || |
1607 |
|
|
(r = (z = b).result) != null) |
1608 |
|
|
return z.uniRunNow(r, e, f); |
1609 |
|
|
|
1610 |
dl |
1.143 |
CompletableFuture<Void> d = newIncompleteFuture(); |
1611 |
jsr166 |
1.200 |
orpush(b, new OrRun<>(e, d, this, b, f)); |
1612 |
dl |
1.113 |
return d; |
1613 |
|
|
} |
1614 |
|
|
|
1615 |
jsr166 |
1.207 |
/** Completion for an anyOf input future. */ |
1616 |
dl |
1.113 |
@SuppressWarnings("serial") |
1617 |
jsr166 |
1.207 |
static class AnyOf extends Completion { |
1618 |
|
|
CompletableFuture<Object> dep; CompletableFuture<?> src; |
1619 |
|
|
CompletableFuture<?>[] srcs; |
1620 |
|
|
AnyOf(CompletableFuture<Object> dep, CompletableFuture<?> src, |
1621 |
|
|
CompletableFuture<?>[] srcs) { |
1622 |
|
|
this.dep = dep; this.src = src; this.srcs = srcs; |
1623 |
dl |
1.113 |
} |
1624 |
jsr166 |
1.124 |
final CompletableFuture<Object> tryFire(int mode) { |
1625 |
jsr166 |
1.207 |
// assert mode != ASYNC; |
1626 |
|
|
CompletableFuture<Object> d; CompletableFuture<?> a; |
1627 |
|
|
CompletableFuture<?>[] as; |
1628 |
jsr166 |
1.201 |
Object r; |
1629 |
|
|
if ((d = dep) == null |
1630 |
jsr166 |
1.207 |
|| (a = src) == null || (r = a.result) == null |
1631 |
|
|
|| (as = srcs) == null) |
1632 |
dl |
1.113 |
return null; |
1633 |
jsr166 |
1.207 |
dep = null; src = null; srcs = null; |
1634 |
|
|
if (d.completeRelay(r)) { |
1635 |
|
|
for (CompletableFuture<?> b : as) |
1636 |
|
|
if (b != a) |
1637 |
|
|
b.cleanStack(); |
1638 |
|
|
if (mode < 0) |
1639 |
|
|
return d; |
1640 |
|
|
else |
1641 |
|
|
d.postComplete(); |
1642 |
|
|
} |
1643 |
|
|
return null; |
1644 |
dl |
1.113 |
} |
1645 |
jsr166 |
1.207 |
final boolean isLive() { |
1646 |
|
|
CompletableFuture<Object> d; |
1647 |
|
|
return (d = dep) != null && d.result == null; |
1648 |
dl |
1.113 |
} |
1649 |
dl |
1.104 |
} |
1650 |
|
|
|
1651 |
dl |
1.113 |
/* ------------- Zero-input Async forms -------------- */ |
1652 |
dl |
1.104 |
|
1653 |
jsr166 |
1.133 |
@SuppressWarnings("serial") |
1654 |
|
|
static final class AsyncSupply<T> extends ForkJoinTask<Void> |
1655 |
dl |
1.143 |
implements Runnable, AsynchronousCompletionTask { |
1656 |
dl |
1.150 |
CompletableFuture<T> dep; Supplier<? extends T> fn; |
1657 |
|
|
AsyncSupply(CompletableFuture<T> dep, Supplier<? extends T> fn) { |
1658 |
dl |
1.113 |
this.dep = dep; this.fn = fn; |
1659 |
|
|
} |
1660 |
|
|
|
1661 |
jsr166 |
1.133 |
public final Void getRawResult() { return null; } |
1662 |
|
|
public final void setRawResult(Void v) {} |
1663 |
dl |
1.187 |
public final boolean exec() { run(); return false; } |
1664 |
jsr166 |
1.133 |
|
1665 |
jsr166 |
1.131 |
public void run() { |
1666 |
dl |
1.150 |
CompletableFuture<T> d; Supplier<? extends T> f; |
1667 |
dl |
1.113 |
if ((d = dep) != null && (f = fn) != null) { |
1668 |
|
|
dep = null; fn = null; |
1669 |
|
|
if (d.result == null) { |
1670 |
|
|
try { |
1671 |
jsr166 |
1.127 |
d.completeValue(f.get()); |
1672 |
dl |
1.113 |
} catch (Throwable ex) { |
1673 |
jsr166 |
1.127 |
d.completeThrowable(ex); |
1674 |
dl |
1.113 |
} |
1675 |
|
|
} |
1676 |
|
|
d.postComplete(); |
1677 |
|
|
} |
1678 |
|
|
} |
1679 |
|
|
} |
1680 |
|
|
|
1681 |
|
|
static <U> CompletableFuture<U> asyncSupplyStage(Executor e, |
1682 |
|
|
Supplier<U> f) { |
1683 |
|
|
if (f == null) throw new NullPointerException(); |
1684 |
|
|
CompletableFuture<U> d = new CompletableFuture<U>(); |
1685 |
|
|
e.execute(new AsyncSupply<U>(d, f)); |
1686 |
|
|
return d; |
1687 |
|
|
} |
1688 |
|
|
|
1689 |
jsr166 |
1.133 |
@SuppressWarnings("serial") |
1690 |
|
|
static final class AsyncRun extends ForkJoinTask<Void> |
1691 |
dl |
1.143 |
implements Runnable, AsynchronousCompletionTask { |
1692 |
dl |
1.113 |
CompletableFuture<Void> dep; Runnable fn; |
1693 |
|
|
AsyncRun(CompletableFuture<Void> dep, Runnable fn) { |
1694 |
|
|
this.dep = dep; this.fn = fn; |
1695 |
|
|
} |
1696 |
|
|
|
1697 |
jsr166 |
1.133 |
public final Void getRawResult() { return null; } |
1698 |
|
|
public final void setRawResult(Void v) {} |
1699 |
dl |
1.187 |
public final boolean exec() { run(); return false; } |
1700 |
jsr166 |
1.133 |
|
1701 |
jsr166 |
1.131 |
public void run() { |
1702 |
dl |
1.113 |
CompletableFuture<Void> d; Runnable f; |
1703 |
|
|
if ((d = dep) != null && (f = fn) != null) { |
1704 |
|
|
dep = null; fn = null; |
1705 |
|
|
if (d.result == null) { |
1706 |
|
|
try { |
1707 |
|
|
f.run(); |
1708 |
jsr166 |
1.128 |
d.completeNull(); |
1709 |
dl |
1.113 |
} catch (Throwable ex) { |
1710 |
jsr166 |
1.127 |
d.completeThrowable(ex); |
1711 |
dl |
1.113 |
} |
1712 |
|
|
} |
1713 |
|
|
d.postComplete(); |
1714 |
dl |
1.88 |
} |
1715 |
|
|
} |
1716 |
|
|
} |
1717 |
|
|
|
1718 |
dl |
1.113 |
static CompletableFuture<Void> asyncRunStage(Executor e, Runnable f) { |
1719 |
|
|
if (f == null) throw new NullPointerException(); |
1720 |
dl |
1.104 |
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1721 |
dl |
1.113 |
e.execute(new AsyncRun(d, f)); |
1722 |
dl |
1.104 |
return d; |
1723 |
|
|
} |
1724 |
|
|
|
1725 |
|
|
/* ------------- Signallers -------------- */ |
1726 |
|
|
|
1727 |
|
|
/** |
1728 |
dl |
1.113 |
* Completion for recording and releasing a waiting thread. This |
1729 |
|
|
* class implements ManagedBlocker to avoid starvation when |
1730 |
|
|
* blocking actions pile up in ForkJoinPools. |
1731 |
dl |
1.104 |
*/ |
1732 |
dl |
1.113 |
@SuppressWarnings("serial") |
1733 |
dl |
1.110 |
static final class Signaller extends Completion |
1734 |
dl |
1.104 |
implements ForkJoinPool.ManagedBlocker { |
1735 |
jsr166 |
1.173 |
long nanos; // remaining wait time if timed |
1736 |
dl |
1.113 |
final long deadline; // non-zero if timed |
1737 |
dl |
1.177 |
final boolean interruptible; |
1738 |
|
|
boolean interrupted; |
1739 |
dl |
1.104 |
volatile Thread thread; |
1740 |
dl |
1.113 |
|
1741 |
dl |
1.104 |
Signaller(boolean interruptible, long nanos, long deadline) { |
1742 |
|
|
this.thread = Thread.currentThread(); |
1743 |
dl |
1.177 |
this.interruptible = interruptible; |
1744 |
dl |
1.104 |
this.nanos = nanos; |
1745 |
|
|
this.deadline = deadline; |
1746 |
|
|
} |
1747 |
dl |
1.113 |
final CompletableFuture<?> tryFire(int ignore) { |
1748 |
|
|
Thread w; // no need to atomically claim |
1749 |
|
|
if ((w = thread) != null) { |
1750 |
|
|
thread = null; |
1751 |
dl |
1.104 |
LockSupport.unpark(w); |
1752 |
dl |
1.88 |
} |
1753 |
dl |
1.104 |
return null; |
1754 |
dl |
1.88 |
} |
1755 |
dl |
1.104 |
public boolean isReleasable() { |
1756 |
dl |
1.177 |
if (Thread.interrupted()) |
1757 |
|
|
interrupted = true; |
1758 |
|
|
return ((interrupted && interruptible) || |
1759 |
|
|
(deadline != 0L && |
1760 |
|
|
(nanos <= 0L || |
1761 |
|
|
(nanos = deadline - System.nanoTime()) <= 0L)) || |
1762 |
|
|
thread == null); |
1763 |
dl |
1.104 |
} |
1764 |
|
|
public boolean block() { |
1765 |
dl |
1.177 |
while (!isReleasable()) { |
1766 |
|
|
if (deadline == 0L) |
1767 |
|
|
LockSupport.park(this); |
1768 |
|
|
else |
1769 |
|
|
LockSupport.parkNanos(this, nanos); |
1770 |
|
|
} |
1771 |
|
|
return true; |
1772 |
dl |
1.88 |
} |
1773 |
dl |
1.113 |
final boolean isLive() { return thread != null; } |
1774 |
dl |
1.88 |
} |
1775 |
|
|
|
1776 |
dl |
1.104 |
/** |
1777 |
|
|
* Returns raw result after waiting, or null if interruptible and |
1778 |
|
|
* interrupted. |
1779 |
|
|
*/ |
1780 |
|
|
private Object waitingGet(boolean interruptible) { |
1781 |
|
|
Signaller q = null; |
1782 |
|
|
boolean queued = false; |
1783 |
dl |
1.88 |
Object r; |
1784 |
dl |
1.104 |
while ((r = result) == null) { |
1785 |
dl |
1.184 |
if (q == null) { |
1786 |
|
|
q = new Signaller(interruptible, 0L, 0L); |
1787 |
dl |
1.186 |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) |
1788 |
|
|
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q); |
1789 |
dl |
1.88 |
} |
1790 |
dl |
1.104 |
else if (!queued) |
1791 |
jsr166 |
1.129 |
queued = tryPushStack(q); |
1792 |
dl |
1.177 |
else { |
1793 |
dl |
1.104 |
try { |
1794 |
|
|
ForkJoinPool.managedBlock(q); |
1795 |
dl |
1.179 |
} catch (InterruptedException ie) { // currently cannot happen |
1796 |
dl |
1.177 |
q.interrupted = true; |
1797 |
dl |
1.104 |
} |
1798 |
dl |
1.177 |
if (q.interrupted && interruptible) |
1799 |
|
|
break; |
1800 |
dl |
1.88 |
} |
1801 |
dl |
1.104 |
} |
1802 |
dl |
1.185 |
if (q != null && queued) { |
1803 |
dl |
1.104 |
q.thread = null; |
1804 |
dl |
1.185 |
if (!interruptible && q.interrupted) |
1805 |
|
|
Thread.currentThread().interrupt(); |
1806 |
|
|
if (r == null) |
1807 |
|
|
cleanStack(); |
1808 |
dl |
1.88 |
} |
1809 |
dl |
1.185 |
if (r != null || (r = result) != null) |
1810 |
dl |
1.177 |
postComplete(); |
1811 |
dl |
1.104 |
return r; |
1812 |
dl |
1.88 |
} |
1813 |
|
|
|
1814 |
dl |
1.104 |
/** |
1815 |
|
|
* Returns raw result after waiting, or null if interrupted, or |
1816 |
|
|
* throws TimeoutException on timeout. |
1817 |
|
|
*/ |
1818 |
|
|
private Object timedGet(long nanos) throws TimeoutException { |
1819 |
|
|
if (Thread.interrupted()) |
1820 |
|
|
return null; |
1821 |
dl |
1.177 |
if (nanos > 0L) { |
1822 |
|
|
long d = System.nanoTime() + nanos; |
1823 |
|
|
long deadline = (d == 0L) ? 1L : d; // avoid 0 |
1824 |
|
|
Signaller q = null; |
1825 |
|
|
boolean queued = false; |
1826 |
|
|
Object r; |
1827 |
dl |
1.184 |
while ((r = result) == null) { // similar to untimed |
1828 |
|
|
if (q == null) { |
1829 |
dl |
1.177 |
q = new Signaller(true, nanos, deadline); |
1830 |
dl |
1.186 |
if (Thread.currentThread() instanceof ForkJoinWorkerThread) |
1831 |
|
|
ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q); |
1832 |
dl |
1.184 |
} |
1833 |
dl |
1.177 |
else if (!queued) |
1834 |
|
|
queued = tryPushStack(q); |
1835 |
jsr166 |
1.178 |
else if (q.nanos <= 0L) |
1836 |
dl |
1.177 |
break; |
1837 |
|
|
else { |
1838 |
|
|
try { |
1839 |
|
|
ForkJoinPool.managedBlock(q); |
1840 |
|
|
} catch (InterruptedException ie) { |
1841 |
|
|
q.interrupted = true; |
1842 |
|
|
} |
1843 |
|
|
if (q.interrupted) |
1844 |
|
|
break; |
1845 |
|
|
} |
1846 |
|
|
} |
1847 |
dl |
1.185 |
if (q != null && queued) { |
1848 |
dl |
1.104 |
q.thread = null; |
1849 |
dl |
1.185 |
if (r == null) |
1850 |
|
|
cleanStack(); |
1851 |
|
|
} |
1852 |
|
|
if (r != null || (r = result) != null) |
1853 |
dl |
1.177 |
postComplete(); |
1854 |
|
|
if (r != null || (q != null && q.interrupted)) |
1855 |
|
|
return r; |
1856 |
dl |
1.88 |
} |
1857 |
dl |
1.177 |
throw new TimeoutException(); |
1858 |
dl |
1.88 |
} |
1859 |
|
|
|
1860 |
dl |
1.104 |
/* ------------- public methods -------------- */ |
1861 |
dl |
1.88 |
|
1862 |
|
|
/** |
1863 |
|
|
* Creates a new incomplete CompletableFuture. |
1864 |
|
|
*/ |
1865 |
|
|
public CompletableFuture() { |
1866 |
|
|
} |
1867 |
|
|
|
1868 |
|
|
/** |
1869 |
jsr166 |
1.128 |
* Creates a new complete CompletableFuture with given encoded result. |
1870 |
|
|
*/ |
1871 |
dl |
1.143 |
CompletableFuture(Object r) { |
1872 |
jsr166 |
1.128 |
this.result = r; |
1873 |
|
|
} |
1874 |
|
|
|
1875 |
|
|
/** |
1876 |
dl |
1.88 |
* Returns a new CompletableFuture that is asynchronously completed |
1877 |
|
|
* by a task running in the {@link ForkJoinPool#commonPool()} with |
1878 |
|
|
* the value obtained by calling the given Supplier. |
1879 |
|
|
* |
1880 |
|
|
* @param supplier a function returning the value to be used |
1881 |
|
|
* to complete the returned CompletableFuture |
1882 |
jsr166 |
1.95 |
* @param <U> the function's return type |
1883 |
dl |
1.88 |
* @return the new CompletableFuture |
1884 |
|
|
*/ |
1885 |
|
|
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { |
1886 |
jsr166 |
1.171 |
return asyncSupplyStage(ASYNC_POOL, supplier); |
1887 |
dl |
1.88 |
} |
1888 |
|
|
|
1889 |
|
|
/** |
1890 |
|
|
* Returns a new CompletableFuture that is asynchronously completed |
1891 |
|
|
* by a task running in the given executor with the value obtained |
1892 |
|
|
* by calling the given Supplier. |
1893 |
|
|
* |
1894 |
|
|
* @param supplier a function returning the value to be used |
1895 |
|
|
* to complete the returned CompletableFuture |
1896 |
|
|
* @param executor the executor to use for asynchronous execution |
1897 |
jsr166 |
1.95 |
* @param <U> the function's return type |
1898 |
dl |
1.88 |
* @return the new CompletableFuture |
1899 |
|
|
*/ |
1900 |
|
|
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, |
1901 |
|
|
Executor executor) { |
1902 |
dl |
1.113 |
return asyncSupplyStage(screenExecutor(executor), supplier); |
1903 |
dl |
1.28 |
} |
1904 |
|
|
|
1905 |
|
|
/** |
1906 |
jsr166 |
1.66 |
* Returns a new CompletableFuture that is asynchronously completed |
1907 |
|
|
* by a task running in the {@link ForkJoinPool#commonPool()} after |
1908 |
|
|
* it runs the given action. |
1909 |
dl |
1.28 |
* |
1910 |
|
|
* @param runnable the action to run before completing the |
1911 |
|
|
* returned CompletableFuture |
1912 |
jsr166 |
1.58 |
* @return the new CompletableFuture |
1913 |
dl |
1.28 |
*/ |
1914 |
|
|
public static CompletableFuture<Void> runAsync(Runnable runnable) { |
1915 |
jsr166 |
1.171 |
return asyncRunStage(ASYNC_POOL, runnable); |
1916 |
dl |
1.28 |
} |
1917 |
|
|
|
1918 |
|
|
/** |
1919 |
jsr166 |
1.66 |
* Returns a new CompletableFuture that is asynchronously completed |
1920 |
|
|
* by a task running in the given executor after it runs the given |
1921 |
|
|
* action. |
1922 |
dl |
1.28 |
* |
1923 |
|
|
* @param runnable the action to run before completing the |
1924 |
|
|
* returned CompletableFuture |
1925 |
|
|
* @param executor the executor to use for asynchronous execution |
1926 |
jsr166 |
1.58 |
* @return the new CompletableFuture |
1927 |
dl |
1.28 |
*/ |
1928 |
|
|
public static CompletableFuture<Void> runAsync(Runnable runnable, |
1929 |
|
|
Executor executor) { |
1930 |
dl |
1.113 |
return asyncRunStage(screenExecutor(executor), runnable); |
1931 |
dl |
1.28 |
} |
1932 |
|
|
|
1933 |
|
|
/** |
1934 |
dl |
1.77 |
* Returns a new CompletableFuture that is already completed with |
1935 |
|
|
* the given value. |
1936 |
|
|
* |
1937 |
|
|
* @param value the value |
1938 |
jsr166 |
1.95 |
* @param <U> the type of the value |
1939 |
dl |
1.77 |
* @return the completed CompletableFuture |
1940 |
|
|
*/ |
1941 |
|
|
public static <U> CompletableFuture<U> completedFuture(U value) { |
1942 |
jsr166 |
1.128 |
return new CompletableFuture<U>((value == null) ? NIL : value); |
1943 |
dl |
1.77 |
} |
1944 |
|
|
|
1945 |
|
|
/** |
1946 |
dl |
1.28 |
* Returns {@code true} if completed in any fashion: normally, |
1947 |
|
|
* exceptionally, or via cancellation. |
1948 |
|
|
* |
1949 |
|
|
* @return {@code true} if completed |
1950 |
|
|
*/ |
1951 |
|
|
public boolean isDone() { |
1952 |
|
|
return result != null; |
1953 |
|
|
} |
1954 |
|
|
|
1955 |
|
|
/** |
1956 |
dl |
1.49 |
* Waits if necessary for this future to complete, and then |
1957 |
dl |
1.48 |
* returns its result. |
1958 |
dl |
1.28 |
* |
1959 |
dl |
1.48 |
* @return the result value |
1960 |
|
|
* @throws CancellationException if this future was cancelled |
1961 |
|
|
* @throws ExecutionException if this future completed exceptionally |
1962 |
dl |
1.28 |
* @throws InterruptedException if the current thread was interrupted |
1963 |
|
|
* while waiting |
1964 |
|
|
*/ |
1965 |
jsr166 |
1.190 |
@SuppressWarnings("unchecked") |
1966 |
dl |
1.28 |
public T get() throws InterruptedException, ExecutionException { |
1967 |
jsr166 |
1.105 |
Object r; |
1968 |
jsr166 |
1.191 |
if ((r = result) == null) |
1969 |
|
|
r = waitingGet(true); |
1970 |
|
|
return (T) reportGet(r); |
1971 |
dl |
1.28 |
} |
1972 |
|
|
|
1973 |
|
|
/** |
1974 |
dl |
1.49 |
* Waits if necessary for at most the given time for this future |
1975 |
|
|
* to complete, and then returns its result, if available. |
1976 |
dl |
1.28 |
* |
1977 |
|
|
* @param timeout the maximum time to wait |
1978 |
|
|
* @param unit the time unit of the timeout argument |
1979 |
dl |
1.48 |
* @return the result value |
1980 |
|
|
* @throws CancellationException if this future was cancelled |
1981 |
|
|
* @throws ExecutionException if this future completed exceptionally |
1982 |
dl |
1.28 |
* @throws InterruptedException if the current thread was interrupted |
1983 |
|
|
* while waiting |
1984 |
|
|
* @throws TimeoutException if the wait timed out |
1985 |
|
|
*/ |
1986 |
jsr166 |
1.190 |
@SuppressWarnings("unchecked") |
1987 |
dl |
1.28 |
public T get(long timeout, TimeUnit unit) |
1988 |
|
|
throws InterruptedException, ExecutionException, TimeoutException { |
1989 |
jsr166 |
1.191 |
long nanos = unit.toNanos(timeout); |
1990 |
jsr166 |
1.105 |
Object r; |
1991 |
jsr166 |
1.191 |
if ((r = result) == null) |
1992 |
|
|
r = timedGet(nanos); |
1993 |
|
|
return (T) reportGet(r); |
1994 |
dl |
1.28 |
} |
1995 |
|
|
|
1996 |
|
|
/** |
1997 |
|
|
* Returns the result value when complete, or throws an |
1998 |
|
|
* (unchecked) exception if completed exceptionally. To better |
1999 |
|
|
* conform with the use of common functional forms, if a |
2000 |
|
|
* computation involved in the completion of this |
2001 |
|
|
* CompletableFuture threw an exception, this method throws an |
2002 |
|
|
* (unchecked) {@link CompletionException} with the underlying |
2003 |
|
|
* exception as its cause. |
2004 |
|
|
* |
2005 |
|
|
* @return the result value |
2006 |
|
|
* @throws CancellationException if the computation was cancelled |
2007 |
jsr166 |
1.55 |
* @throws CompletionException if this future completed |
2008 |
|
|
* exceptionally or a completion computation threw an exception |
2009 |
dl |
1.28 |
*/ |
2010 |
jsr166 |
1.190 |
@SuppressWarnings("unchecked") |
2011 |
dl |
1.28 |
public T join() { |
2012 |
dl |
1.104 |
Object r; |
2013 |
jsr166 |
1.191 |
if ((r = result) == null) |
2014 |
|
|
r = waitingGet(false); |
2015 |
|
|
return (T) reportJoin(r); |
2016 |
dl |
1.28 |
} |
2017 |
|
|
|
2018 |
|
|
/** |
2019 |
|
|
* Returns the result value (or throws any encountered exception) |
2020 |
|
|
* if completed, else returns the given valueIfAbsent. |
2021 |
|
|
* |
2022 |
|
|
* @param valueIfAbsent the value to return if not completed |
2023 |
|
|
* @return the result value, if completed, else the given valueIfAbsent |
2024 |
|
|
* @throws CancellationException if the computation was cancelled |
2025 |
jsr166 |
1.55 |
* @throws CompletionException if this future completed |
2026 |
|
|
* exceptionally or a completion computation threw an exception |
2027 |
dl |
1.28 |
*/ |
2028 |
jsr166 |
1.190 |
@SuppressWarnings("unchecked") |
2029 |
dl |
1.28 |
public T getNow(T valueIfAbsent) { |
2030 |
dl |
1.104 |
Object r; |
2031 |
jsr166 |
1.190 |
return ((r = result) == null) ? valueIfAbsent : (T) reportJoin(r); |
2032 |
dl |
1.28 |
} |
2033 |
|
|
|
2034 |
|
|
/** |
2035 |
|
|
* If not already completed, sets the value returned by {@link |
2036 |
|
|
* #get()} and related methods to the given value. |
2037 |
|
|
* |
2038 |
|
|
* @param value the result value |
2039 |
|
|
* @return {@code true} if this invocation caused this CompletableFuture |
2040 |
|
|
* to transition to a completed state, else {@code false} |
2041 |
|
|
*/ |
2042 |
|
|
public boolean complete(T value) { |
2043 |
jsr166 |
1.127 |
boolean triggered = completeValue(value); |
2044 |
dl |
1.104 |
postComplete(); |
2045 |
dl |
1.28 |
return triggered; |
2046 |
|
|
} |
2047 |
|
|
|
2048 |
|
|
/** |
2049 |
|
|
* If not already completed, causes invocations of {@link #get()} |
2050 |
|
|
* and related methods to throw the given exception. |
2051 |
|
|
* |
2052 |
|
|
* @param ex the exception |
2053 |
|
|
* @return {@code true} if this invocation caused this CompletableFuture |
2054 |
|
|
* to transition to a completed state, else {@code false} |
2055 |
|
|
*/ |
2056 |
|
|
public boolean completeExceptionally(Throwable ex) { |
2057 |
|
|
if (ex == null) throw new NullPointerException(); |
2058 |
dl |
1.104 |
boolean triggered = internalComplete(new AltResult(ex)); |
2059 |
|
|
postComplete(); |
2060 |
dl |
1.28 |
return triggered; |
2061 |
|
|
} |
2062 |
|
|
|
2063 |
dl |
1.104 |
public <U> CompletableFuture<U> thenApply( |
2064 |
|
|
Function<? super T,? extends U> fn) { |
2065 |
dl |
1.113 |
return uniApplyStage(null, fn); |
2066 |
dl |
1.28 |
} |
2067 |
|
|
|
2068 |
dl |
1.104 |
public <U> CompletableFuture<U> thenApplyAsync( |
2069 |
|
|
Function<? super T,? extends U> fn) { |
2070 |
dl |
1.143 |
return uniApplyStage(defaultExecutor(), fn); |
2071 |
dl |
1.17 |
} |
2072 |
|
|
|
2073 |
dl |
1.104 |
public <U> CompletableFuture<U> thenApplyAsync( |
2074 |
|
|
Function<? super T,? extends U> fn, Executor executor) { |
2075 |
dl |
1.113 |
return uniApplyStage(screenExecutor(executor), fn); |
2076 |
dl |
1.28 |
} |
2077 |
dl |
1.1 |
|
2078 |
dl |
1.104 |
public CompletableFuture<Void> thenAccept(Consumer<? super T> action) { |
2079 |
dl |
1.113 |
return uniAcceptStage(null, action); |
2080 |
dl |
1.28 |
} |
2081 |
|
|
|
2082 |
dl |
1.104 |
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) { |
2083 |
dl |
1.143 |
return uniAcceptStage(defaultExecutor(), action); |
2084 |
dl |
1.28 |
} |
2085 |
|
|
|
2086 |
dl |
1.113 |
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, |
2087 |
|
|
Executor executor) { |
2088 |
|
|
return uniAcceptStage(screenExecutor(executor), action); |
2089 |
dl |
1.7 |
} |
2090 |
|
|
|
2091 |
dl |
1.104 |
public CompletableFuture<Void> thenRun(Runnable action) { |
2092 |
dl |
1.113 |
return uniRunStage(null, action); |
2093 |
dl |
1.28 |
} |
2094 |
|
|
|
2095 |
dl |
1.104 |
public CompletableFuture<Void> thenRunAsync(Runnable action) { |
2096 |
dl |
1.143 |
return uniRunStage(defaultExecutor(), action); |
2097 |
dl |
1.28 |
} |
2098 |
|
|
|
2099 |
dl |
1.113 |
public CompletableFuture<Void> thenRunAsync(Runnable action, |
2100 |
|
|
Executor executor) { |
2101 |
|
|
return uniRunStage(screenExecutor(executor), action); |
2102 |
dl |
1.28 |
} |
2103 |
|
|
|
2104 |
dl |
1.104 |
public <U,V> CompletableFuture<V> thenCombine( |
2105 |
|
|
CompletionStage<? extends U> other, |
2106 |
|
|
BiFunction<? super T,? super U,? extends V> fn) { |
2107 |
dl |
1.113 |
return biApplyStage(null, other, fn); |
2108 |
dl |
1.28 |
} |
2109 |
|
|
|
2110 |
dl |
1.104 |
public <U,V> CompletableFuture<V> thenCombineAsync( |
2111 |
|
|
CompletionStage<? extends U> other, |
2112 |
|
|
BiFunction<? super T,? super U,? extends V> fn) { |
2113 |
dl |
1.143 |
return biApplyStage(defaultExecutor(), other, fn); |
2114 |
dl |
1.28 |
} |
2115 |
|
|
|
2116 |
dl |
1.104 |
public <U,V> CompletableFuture<V> thenCombineAsync( |
2117 |
|
|
CompletionStage<? extends U> other, |
2118 |
dl |
1.113 |
BiFunction<? super T,? super U,? extends V> fn, Executor executor) { |
2119 |
|
|
return biApplyStage(screenExecutor(executor), other, fn); |
2120 |
dl |
1.1 |
} |
2121 |
|
|
|
2122 |
dl |
1.104 |
public <U> CompletableFuture<Void> thenAcceptBoth( |
2123 |
|
|
CompletionStage<? extends U> other, |
2124 |
|
|
BiConsumer<? super T, ? super U> action) { |
2125 |
dl |
1.113 |
return biAcceptStage(null, other, action); |
2126 |
dl |
1.28 |
} |
2127 |
|
|
|
2128 |
dl |
1.104 |
public <U> CompletableFuture<Void> thenAcceptBothAsync( |
2129 |
|
|
CompletionStage<? extends U> other, |
2130 |
|
|
BiConsumer<? super T, ? super U> action) { |
2131 |
dl |
1.143 |
return biAcceptStage(defaultExecutor(), other, action); |
2132 |
dl |
1.28 |
} |
2133 |
|
|
|
2134 |
dl |
1.104 |
public <U> CompletableFuture<Void> thenAcceptBothAsync( |
2135 |
|
|
CompletionStage<? extends U> other, |
2136 |
dl |
1.113 |
BiConsumer<? super T, ? super U> action, Executor executor) { |
2137 |
|
|
return biAcceptStage(screenExecutor(executor), other, action); |
2138 |
dl |
1.28 |
} |
2139 |
|
|
|
2140 |
dl |
1.113 |
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, |
2141 |
|
|
Runnable action) { |
2142 |
|
|
return biRunStage(null, other, action); |
2143 |
dl |
1.7 |
} |
2144 |
|
|
|
2145 |
dl |
1.113 |
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, |
2146 |
|
|
Runnable action) { |
2147 |
dl |
1.143 |
return biRunStage(defaultExecutor(), other, action); |
2148 |
dl |
1.28 |
} |
2149 |
|
|
|
2150 |
dl |
1.113 |
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, |
2151 |
|
|
Runnable action, |
2152 |
|
|
Executor executor) { |
2153 |
|
|
return biRunStage(screenExecutor(executor), other, action); |
2154 |
dl |
1.28 |
} |
2155 |
|
|
|
2156 |
dl |
1.104 |
public <U> CompletableFuture<U> applyToEither( |
2157 |
|
|
CompletionStage<? extends T> other, Function<? super T, U> fn) { |
2158 |
dl |
1.113 |
return orApplyStage(null, other, fn); |
2159 |
dl |
1.28 |
} |
2160 |
|
|
|
2161 |
dl |
1.104 |
public <U> CompletableFuture<U> applyToEitherAsync( |
2162 |
|
|
CompletionStage<? extends T> other, Function<? super T, U> fn) { |
2163 |
dl |
1.143 |
return orApplyStage(defaultExecutor(), other, fn); |
2164 |
dl |
1.28 |
} |
2165 |
|
|
|
2166 |
dl |
1.113 |
public <U> CompletableFuture<U> applyToEitherAsync( |
2167 |
|
|
CompletionStage<? extends T> other, Function<? super T, U> fn, |
2168 |
|
|
Executor executor) { |
2169 |
|
|
return orApplyStage(screenExecutor(executor), other, fn); |
2170 |
dl |
1.1 |
} |
2171 |
|
|
|
2172 |
dl |
1.104 |
public CompletableFuture<Void> acceptEither( |
2173 |
|
|
CompletionStage<? extends T> other, Consumer<? super T> action) { |
2174 |
dl |
1.113 |
return orAcceptStage(null, other, action); |
2175 |
dl |
1.28 |
} |
2176 |
|
|
|
2177 |
dl |
1.113 |
public CompletableFuture<Void> acceptEitherAsync( |
2178 |
|
|
CompletionStage<? extends T> other, Consumer<? super T> action) { |
2179 |
dl |
1.143 |
return orAcceptStage(defaultExecutor(), other, action); |
2180 |
dl |
1.28 |
} |
2181 |
|
|
|
2182 |
dl |
1.104 |
public CompletableFuture<Void> acceptEitherAsync( |
2183 |
|
|
CompletionStage<? extends T> other, Consumer<? super T> action, |
2184 |
|
|
Executor executor) { |
2185 |
dl |
1.113 |
return orAcceptStage(screenExecutor(executor), other, action); |
2186 |
dl |
1.7 |
} |
2187 |
|
|
|
2188 |
dl |
1.113 |
public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, |
2189 |
|
|
Runnable action) { |
2190 |
|
|
return orRunStage(null, other, action); |
2191 |
dl |
1.28 |
} |
2192 |
|
|
|
2193 |
dl |
1.113 |
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, |
2194 |
|
|
Runnable action) { |
2195 |
dl |
1.143 |
return orRunStage(defaultExecutor(), other, action); |
2196 |
dl |
1.28 |
} |
2197 |
|
|
|
2198 |
dl |
1.113 |
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, |
2199 |
|
|
Runnable action, |
2200 |
|
|
Executor executor) { |
2201 |
|
|
return orRunStage(screenExecutor(executor), other, action); |
2202 |
dl |
1.1 |
} |
2203 |
|
|
|
2204 |
dl |
1.113 |
public <U> CompletableFuture<U> thenCompose( |
2205 |
|
|
Function<? super T, ? extends CompletionStage<U>> fn) { |
2206 |
|
|
return uniComposeStage(null, fn); |
2207 |
dl |
1.37 |
} |
2208 |
|
|
|
2209 |
dl |
1.104 |
public <U> CompletableFuture<U> thenComposeAsync( |
2210 |
|
|
Function<? super T, ? extends CompletionStage<U>> fn) { |
2211 |
dl |
1.143 |
return uniComposeStage(defaultExecutor(), fn); |
2212 |
dl |
1.37 |
} |
2213 |
|
|
|
2214 |
dl |
1.104 |
public <U> CompletableFuture<U> thenComposeAsync( |
2215 |
|
|
Function<? super T, ? extends CompletionStage<U>> fn, |
2216 |
|
|
Executor executor) { |
2217 |
dl |
1.113 |
return uniComposeStage(screenExecutor(executor), fn); |
2218 |
dl |
1.37 |
} |
2219 |
|
|
|
2220 |
dl |
1.104 |
public CompletableFuture<T> whenComplete( |
2221 |
|
|
BiConsumer<? super T, ? super Throwable> action) { |
2222 |
dl |
1.113 |
return uniWhenCompleteStage(null, action); |
2223 |
dl |
1.88 |
} |
2224 |
|
|
|
2225 |
dl |
1.104 |
public CompletableFuture<T> whenCompleteAsync( |
2226 |
|
|
BiConsumer<? super T, ? super Throwable> action) { |
2227 |
dl |
1.143 |
return uniWhenCompleteStage(defaultExecutor(), action); |
2228 |
dl |
1.88 |
} |
2229 |
|
|
|
2230 |
dl |
1.104 |
public CompletableFuture<T> whenCompleteAsync( |
2231 |
|
|
BiConsumer<? super T, ? super Throwable> action, Executor executor) { |
2232 |
dl |
1.113 |
return uniWhenCompleteStage(screenExecutor(executor), action); |
2233 |
dl |
1.88 |
} |
2234 |
|
|
|
2235 |
dl |
1.104 |
public <U> CompletableFuture<U> handle( |
2236 |
|
|
BiFunction<? super T, Throwable, ? extends U> fn) { |
2237 |
dl |
1.113 |
return uniHandleStage(null, fn); |
2238 |
dl |
1.88 |
} |
2239 |
|
|
|
2240 |
dl |
1.104 |
public <U> CompletableFuture<U> handleAsync( |
2241 |
|
|
BiFunction<? super T, Throwable, ? extends U> fn) { |
2242 |
dl |
1.143 |
return uniHandleStage(defaultExecutor(), fn); |
2243 |
dl |
1.88 |
} |
2244 |
|
|
|
2245 |
dl |
1.104 |
public <U> CompletableFuture<U> handleAsync( |
2246 |
|
|
BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) { |
2247 |
dl |
1.113 |
return uniHandleStage(screenExecutor(executor), fn); |
2248 |
dl |
1.88 |
} |
2249 |
|
|
|
2250 |
|
|
/** |
2251 |
jsr166 |
1.108 |
* Returns this CompletableFuture. |
2252 |
dl |
1.88 |
* |
2253 |
|
|
* @return this CompletableFuture |
2254 |
|
|
*/ |
2255 |
|
|
public CompletableFuture<T> toCompletableFuture() { |
2256 |
|
|
return this; |
2257 |
dl |
1.28 |
} |
2258 |
|
|
|
2259 |
dl |
1.88 |
// not in interface CompletionStage |
2260 |
|
|
|
2261 |
dl |
1.28 |
/** |
2262 |
jsr166 |
1.66 |
* Returns a new CompletableFuture that is completed when this |
2263 |
|
|
* CompletableFuture completes, with the result of the given |
2264 |
|
|
* function of the exception triggering this CompletableFuture's |
2265 |
|
|
* completion when it completes exceptionally; otherwise, if this |
2266 |
|
|
* CompletableFuture completes normally, then the returned |
2267 |
|
|
* CompletableFuture also completes normally with the same value. |
2268 |
dl |
1.88 |
* Note: More flexible versions of this functionality are |
2269 |
|
|
* available using methods {@code whenComplete} and {@code handle}. |
2270 |
dl |
1.28 |
* |
2271 |
|
|
* @param fn the function to use to compute the value of the |
2272 |
|
|
* returned CompletableFuture if this CompletableFuture completed |
2273 |
|
|
* exceptionally |
2274 |
|
|
* @return the new CompletableFuture |
2275 |
|
|
*/ |
2276 |
dl |
1.104 |
public CompletableFuture<T> exceptionally( |
2277 |
|
|
Function<Throwable, ? extends T> fn) { |
2278 |
dl |
1.113 |
return uniExceptionallyStage(fn); |
2279 |
dl |
1.28 |
} |
2280 |
|
|
|
2281 |
dl |
1.143 |
|
2282 |
dl |
1.35 |
/* ------------- Arbitrary-arity constructions -------------- */ |
2283 |
|
|
|
2284 |
|
|
/** |
2285 |
|
|
* Returns a new CompletableFuture that is completed when all of |
2286 |
jsr166 |
1.66 |
* the given CompletableFutures complete. If any of the given |
2287 |
jsr166 |
1.69 |
* CompletableFutures complete exceptionally, then the returned |
2288 |
|
|
* CompletableFuture also does so, with a CompletionException |
2289 |
|
|
* holding this exception as its cause. Otherwise, the results, |
2290 |
|
|
* if any, of the given CompletableFutures are not reflected in |
2291 |
|
|
* the returned CompletableFuture, but may be obtained by |
2292 |
|
|
* inspecting them individually. If no CompletableFutures are |
2293 |
|
|
* provided, returns a CompletableFuture completed with the value |
2294 |
|
|
* {@code null}. |
2295 |
dl |
1.35 |
* |
2296 |
|
|
* <p>Among the applications of this method is to await completion |
2297 |
|
|
* of a set of independent CompletableFutures before continuing a |
2298 |
|
|
* program, as in: {@code CompletableFuture.allOf(c1, c2, |
2299 |
|
|
* c3).join();}. |
2300 |
|
|
* |
2301 |
|
|
* @param cfs the CompletableFutures |
2302 |
jsr166 |
1.59 |
* @return a new CompletableFuture that is completed when all of the |
2303 |
dl |
1.35 |
* given CompletableFutures complete |
2304 |
|
|
* @throws NullPointerException if the array or any of its elements are |
2305 |
|
|
* {@code null} |
2306 |
|
|
*/ |
2307 |
|
|
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { |
2308 |
dl |
1.113 |
return andTree(cfs, 0, cfs.length - 1); |
2309 |
dl |
1.35 |
} |
2310 |
|
|
|
2311 |
|
|
/** |
2312 |
dl |
1.76 |
* Returns a new CompletableFuture that is completed when any of |
2313 |
jsr166 |
1.79 |
* the given CompletableFutures complete, with the same result. |
2314 |
|
|
* Otherwise, if it completed exceptionally, the returned |
2315 |
dl |
1.77 |
* CompletableFuture also does so, with a CompletionException |
2316 |
|
|
* holding this exception as its cause. If no CompletableFutures |
2317 |
|
|
* are provided, returns an incomplete CompletableFuture. |
2318 |
dl |
1.35 |
* |
2319 |
|
|
* @param cfs the CompletableFutures |
2320 |
dl |
1.77 |
* @return a new CompletableFuture that is completed with the |
2321 |
|
|
* result or exception of any of the given CompletableFutures when |
2322 |
|
|
* one completes |
2323 |
dl |
1.35 |
* @throws NullPointerException if the array or any of its elements are |
2324 |
|
|
* {@code null} |
2325 |
|
|
*/ |
2326 |
dl |
1.77 |
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { |
2327 |
jsr166 |
1.207 |
int n; Object r; |
2328 |
|
|
if ((n = cfs.length) <= 1) |
2329 |
|
|
return (n == 0) |
2330 |
|
|
? new CompletableFuture<Object>() |
2331 |
|
|
: uniCopyStage(cfs[0]); |
2332 |
|
|
for (CompletableFuture<?> cf : cfs) |
2333 |
|
|
if ((r = cf.result) != null) |
2334 |
|
|
return new CompletableFuture<Object>(encodeRelay(r)); |
2335 |
|
|
cfs = cfs.clone(); |
2336 |
|
|
CompletableFuture<Object> d = new CompletableFuture<>(); |
2337 |
|
|
for (CompletableFuture<?> cf : cfs) |
2338 |
|
|
cf.unipush(new AnyOf(d, cf, cfs)); |
2339 |
|
|
// If d was completed while we were adding completions, we should |
2340 |
|
|
// clean the stack of any sources that may have had completions |
2341 |
|
|
// pushed on their stack after d was completed. |
2342 |
|
|
if (d.result != null) |
2343 |
|
|
for (int i = 0, len = cfs.length; i < len; i++) |
2344 |
|
|
if (cfs[i].result != null) |
2345 |
|
|
for (i++; i < len; i++) |
2346 |
|
|
if (cfs[i].result == null) |
2347 |
|
|
cfs[i].cleanStack(); |
2348 |
|
|
return d; |
2349 |
dl |
1.35 |
} |
2350 |
|
|
|
2351 |
|
|
/* ------------- Control and status methods -------------- */ |
2352 |
|
|
|
2353 |
dl |
1.28 |
/** |
2354 |
dl |
1.37 |
* If not already completed, completes this CompletableFuture with |
2355 |
|
|
* a {@link CancellationException}. Dependent CompletableFutures |
2356 |
|
|
* that have not already completed will also complete |
2357 |
|
|
* exceptionally, with a {@link CompletionException} caused by |
2358 |
|
|
* this {@code CancellationException}. |
2359 |
dl |
1.28 |
* |
2360 |
|
|
* @param mayInterruptIfRunning this value has no effect in this |
2361 |
|
|
* implementation because interrupts are not used to control |
2362 |
|
|
* processing. |
2363 |
|
|
* |
2364 |
|
|
* @return {@code true} if this task is now cancelled |
2365 |
|
|
*/ |
2366 |
|
|
public boolean cancel(boolean mayInterruptIfRunning) { |
2367 |
dl |
1.46 |
boolean cancelled = (result == null) && |
2368 |
dl |
1.104 |
internalComplete(new AltResult(new CancellationException())); |
2369 |
|
|
postComplete(); |
2370 |
dl |
1.48 |
return cancelled || isCancelled(); |
2371 |
dl |
1.28 |
} |
2372 |
|
|
|
2373 |
|
|
/** |
2374 |
|
|
* Returns {@code true} if this CompletableFuture was cancelled |
2375 |
|
|
* before it completed normally. |
2376 |
|
|
* |
2377 |
|
|
* @return {@code true} if this CompletableFuture was cancelled |
2378 |
|
|
* before it completed normally |
2379 |
|
|
*/ |
2380 |
|
|
public boolean isCancelled() { |
2381 |
|
|
Object r; |
2382 |
jsr166 |
1.43 |
return ((r = result) instanceof AltResult) && |
2383 |
|
|
(((AltResult)r).ex instanceof CancellationException); |
2384 |
dl |
1.28 |
} |
2385 |
|
|
|
2386 |
|
|
/** |
2387 |
dl |
1.88 |
* Returns {@code true} if this CompletableFuture completed |
2388 |
dl |
1.91 |
* exceptionally, in any way. Possible causes include |
2389 |
|
|
* cancellation, explicit invocation of {@code |
2390 |
|
|
* completeExceptionally}, and abrupt termination of a |
2391 |
|
|
* CompletionStage action. |
2392 |
dl |
1.88 |
* |
2393 |
|
|
* @return {@code true} if this CompletableFuture completed |
2394 |
|
|
* exceptionally |
2395 |
|
|
*/ |
2396 |
|
|
public boolean isCompletedExceptionally() { |
2397 |
dl |
1.91 |
Object r; |
2398 |
|
|
return ((r = result) instanceof AltResult) && r != NIL; |
2399 |
dl |
1.88 |
} |
2400 |
|
|
|
2401 |
|
|
/** |
2402 |
dl |
1.28 |
* Forcibly sets or resets the value subsequently returned by |
2403 |
jsr166 |
1.42 |
* method {@link #get()} and related methods, whether or not |
2404 |
|
|
* already completed. This method is designed for use only in |
2405 |
|
|
* error recovery actions, and even in such situations may result |
2406 |
|
|
* in ongoing dependent completions using established versus |
2407 |
dl |
1.30 |
* overwritten outcomes. |
2408 |
dl |
1.28 |
* |
2409 |
|
|
* @param value the completion value |
2410 |
|
|
*/ |
2411 |
|
|
public void obtrudeValue(T value) { |
2412 |
|
|
result = (value == null) ? NIL : value; |
2413 |
dl |
1.104 |
postComplete(); |
2414 |
dl |
1.28 |
} |
2415 |
|
|
|
2416 |
dl |
1.30 |
/** |
2417 |
jsr166 |
1.41 |
* Forcibly causes subsequent invocations of method {@link #get()} |
2418 |
|
|
* and related methods to throw the given exception, whether or |
2419 |
|
|
* not already completed. This method is designed for use only in |
2420 |
jsr166 |
1.119 |
* error recovery actions, and even in such situations may result |
2421 |
|
|
* in ongoing dependent completions using established versus |
2422 |
dl |
1.30 |
* overwritten outcomes. |
2423 |
|
|
* |
2424 |
|
|
* @param ex the exception |
2425 |
jsr166 |
1.120 |
* @throws NullPointerException if the exception is null |
2426 |
dl |
1.30 |
*/ |
2427 |
|
|
public void obtrudeException(Throwable ex) { |
2428 |
|
|
if (ex == null) throw new NullPointerException(); |
2429 |
|
|
result = new AltResult(ex); |
2430 |
dl |
1.104 |
postComplete(); |
2431 |
dl |
1.30 |
} |
2432 |
|
|
|
2433 |
dl |
1.35 |
/** |
2434 |
|
|
* Returns the estimated number of CompletableFutures whose |
2435 |
|
|
* completions are awaiting completion of this CompletableFuture. |
2436 |
|
|
* This method is designed for use in monitoring system state, not |
2437 |
|
|
* for synchronization control. |
2438 |
|
|
* |
2439 |
|
|
* @return the number of dependent CompletableFutures |
2440 |
|
|
*/ |
2441 |
|
|
public int getNumberOfDependents() { |
2442 |
|
|
int count = 0; |
2443 |
dl |
1.113 |
for (Completion p = stack; p != null; p = p.next) |
2444 |
dl |
1.35 |
++count; |
2445 |
|
|
return count; |
2446 |
|
|
} |
2447 |
|
|
|
2448 |
|
|
/** |
2449 |
|
|
* Returns a string identifying this CompletableFuture, as well as |
2450 |
jsr166 |
1.40 |
* its completion state. The state, in brackets, contains the |
2451 |
dl |
1.35 |
* String {@code "Completed Normally"} or the String {@code |
2452 |
|
|
* "Completed Exceptionally"}, or the String {@code "Not |
2453 |
|
|
* completed"} followed by the number of CompletableFutures |
2454 |
|
|
* dependent upon its completion, if any. |
2455 |
|
|
* |
2456 |
|
|
* @return a string identifying this CompletableFuture, as well as its state |
2457 |
|
|
*/ |
2458 |
|
|
public String toString() { |
2459 |
|
|
Object r = result; |
2460 |
dl |
1.143 |
int count = 0; // avoid call to getNumberOfDependents in case disabled |
2461 |
|
|
for (Completion p = stack; p != null; p = p.next) |
2462 |
|
|
++count; |
2463 |
jsr166 |
1.40 |
return super.toString() + |
2464 |
|
|
((r == null) ? |
2465 |
dl |
1.143 |
((count == 0) ? |
2466 |
jsr166 |
1.40 |
"[Not completed]" : |
2467 |
|
|
"[Not completed, " + count + " dependents]") : |
2468 |
|
|
(((r instanceof AltResult) && ((AltResult)r).ex != null) ? |
2469 |
|
|
"[Completed exceptionally]" : |
2470 |
|
|
"[Completed normally]")); |
2471 |
dl |
1.35 |
} |
2472 |
|
|
|
2473 |
dl |
1.143 |
// jdk9 additions |
2474 |
|
|
|
2475 |
|
|
/** |
2476 |
jsr166 |
1.152 |
* Returns a new incomplete CompletableFuture of the type to be |
2477 |
dl |
1.143 |
* returned by a CompletionStage method. Subclasses should |
2478 |
|
|
* normally override this method to return an instance of the same |
2479 |
|
|
* class as this CompletableFuture. The default implementation |
2480 |
|
|
* returns an instance of class CompletableFuture. |
2481 |
|
|
* |
2482 |
jsr166 |
1.148 |
* @param <U> the type of the value |
2483 |
dl |
1.143 |
* @return a new CompletableFuture |
2484 |
jsr166 |
1.182 |
* @since 9 |
2485 |
dl |
1.143 |
*/ |
2486 |
|
|
public <U> CompletableFuture<U> newIncompleteFuture() { |
2487 |
|
|
return new CompletableFuture<U>(); |
2488 |
|
|
} |
2489 |
jsr166 |
1.147 |
|
2490 |
dl |
1.143 |
/** |
2491 |
|
|
* Returns the default Executor used for async methods that do not |
2492 |
|
|
* specify an Executor. This class uses the {@link |
2493 |
dl |
1.161 |
* ForkJoinPool#commonPool()} if it supports more than one |
2494 |
|
|
* parallel thread, or else an Executor using one thread per async |
2495 |
jsr166 |
1.165 |
* task. This method may be overridden in subclasses to return |
2496 |
dl |
1.161 |
* an Executor that provides at least one independent thread. |
2497 |
dl |
1.143 |
* |
2498 |
|
|
* @return the executor |
2499 |
jsr166 |
1.182 |
* @since 9 |
2500 |
dl |
1.143 |
*/ |
2501 |
|
|
public Executor defaultExecutor() { |
2502 |
jsr166 |
1.171 |
return ASYNC_POOL; |
2503 |
dl |
1.143 |
} |
2504 |
|
|
|
2505 |
|
|
/** |
2506 |
|
|
* Returns a new CompletableFuture that is completed normally with |
2507 |
jsr166 |
1.144 |
* the same value as this CompletableFuture when it completes |
2508 |
dl |
1.143 |
* normally. If this CompletableFuture completes exceptionally, |
2509 |
|
|
* then the returned CompletableFuture completes exceptionally |
2510 |
|
|
* with a CompletionException with this exception as cause. The |
2511 |
jsr166 |
1.145 |
* behavior is equivalent to {@code thenApply(x -> x)}. This |
2512 |
dl |
1.143 |
* method may be useful as a form of "defensive copying", to |
2513 |
|
|
* prevent clients from completing, while still being able to |
2514 |
|
|
* arrange dependent actions. |
2515 |
|
|
* |
2516 |
|
|
* @return the new CompletableFuture |
2517 |
jsr166 |
1.182 |
* @since 9 |
2518 |
dl |
1.143 |
*/ |
2519 |
|
|
public CompletableFuture<T> copy() { |
2520 |
jsr166 |
1.207 |
return uniCopyStage(this); |
2521 |
dl |
1.143 |
} |
2522 |
|
|
|
2523 |
|
|
/** |
2524 |
|
|
* Returns a new CompletionStage that is completed normally with |
2525 |
jsr166 |
1.144 |
* the same value as this CompletableFuture when it completes |
2526 |
dl |
1.143 |
* normally, and cannot be independently completed or otherwise |
2527 |
|
|
* used in ways not defined by the methods of interface {@link |
2528 |
|
|
* CompletionStage}. If this CompletableFuture completes |
2529 |
|
|
* exceptionally, then the returned CompletionStage completes |
2530 |
|
|
* exceptionally with a CompletionException with this exception as |
2531 |
|
|
* cause. |
2532 |
|
|
* |
2533 |
jsr166 |
1.210 |
* <p>Unless overridden by a subclass, a new non-minimal |
2534 |
|
|
* CompletableFuture with all methods available can be obtained from |
2535 |
|
|
* a minimal CompletionStage via {@link #toCompletableFuture()}. |
2536 |
|
|
* For example, completion of a minimal stage can be awaited by |
2537 |
|
|
* |
2538 |
|
|
* <pre> {@code minimalStage.toCompletableFuture().join(); }</pre> |
2539 |
|
|
* |
2540 |
dl |
1.143 |
* @return the new CompletionStage |
2541 |
jsr166 |
1.182 |
* @since 9 |
2542 |
dl |
1.143 |
*/ |
2543 |
|
|
public CompletionStage<T> minimalCompletionStage() { |
2544 |
|
|
return uniAsMinimalStage(); |
2545 |
|
|
} |
2546 |
|
|
|
2547 |
|
|
/** |
2548 |
|
|
* Completes this CompletableFuture with the result of |
2549 |
|
|
* the given Supplier function invoked from an asynchronous |
2550 |
|
|
* task using the given executor. |
2551 |
|
|
* |
2552 |
|
|
* @param supplier a function returning the value to be used |
2553 |
|
|
* to complete this CompletableFuture |
2554 |
|
|
* @param executor the executor to use for asynchronous execution |
2555 |
|
|
* @return this CompletableFuture |
2556 |
jsr166 |
1.182 |
* @since 9 |
2557 |
dl |
1.143 |
*/ |
2558 |
dl |
1.150 |
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier, |
2559 |
dl |
1.143 |
Executor executor) { |
2560 |
|
|
if (supplier == null || executor == null) |
2561 |
|
|
throw new NullPointerException(); |
2562 |
|
|
executor.execute(new AsyncSupply<T>(this, supplier)); |
2563 |
|
|
return this; |
2564 |
|
|
} |
2565 |
|
|
|
2566 |
|
|
/** |
2567 |
|
|
* Completes this CompletableFuture with the result of the given |
2568 |
|
|
* Supplier function invoked from an asynchronous task using the |
2569 |
jsr166 |
1.154 |
* default executor. |
2570 |
dl |
1.143 |
* |
2571 |
|
|
* @param supplier a function returning the value to be used |
2572 |
|
|
* to complete this CompletableFuture |
2573 |
|
|
* @return this CompletableFuture |
2574 |
jsr166 |
1.182 |
* @since 9 |
2575 |
dl |
1.143 |
*/ |
2576 |
dl |
1.150 |
public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier) { |
2577 |
dl |
1.143 |
return completeAsync(supplier, defaultExecutor()); |
2578 |
|
|
} |
2579 |
|
|
|
2580 |
|
|
/** |
2581 |
|
|
* Exceptionally completes this CompletableFuture with |
2582 |
|
|
* a {@link TimeoutException} if not otherwise completed |
2583 |
|
|
* before the given timeout. |
2584 |
|
|
* |
2585 |
|
|
* @param timeout how long to wait before completing exceptionally |
2586 |
|
|
* with a TimeoutException, in units of {@code unit} |
2587 |
|
|
* @param unit a {@code TimeUnit} determining how to interpret the |
2588 |
|
|
* {@code timeout} parameter |
2589 |
|
|
* @return this CompletableFuture |
2590 |
jsr166 |
1.182 |
* @since 9 |
2591 |
dl |
1.143 |
*/ |
2592 |
|
|
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) { |
2593 |
dl |
1.158 |
if (unit == null) |
2594 |
|
|
throw new NullPointerException(); |
2595 |
dl |
1.143 |
if (result == null) |
2596 |
|
|
whenComplete(new Canceller(Delayer.delay(new Timeout(this), |
2597 |
|
|
timeout, unit))); |
2598 |
|
|
return this; |
2599 |
|
|
} |
2600 |
|
|
|
2601 |
|
|
/** |
2602 |
dl |
1.146 |
* Completes this CompletableFuture with the given value if not |
2603 |
|
|
* otherwise completed before the given timeout. |
2604 |
|
|
* |
2605 |
|
|
* @param value the value to use upon timeout |
2606 |
jsr166 |
1.152 |
* @param timeout how long to wait before completing normally |
2607 |
|
|
* with the given value, in units of {@code unit} |
2608 |
dl |
1.146 |
* @param unit a {@code TimeUnit} determining how to interpret the |
2609 |
|
|
* {@code timeout} parameter |
2610 |
|
|
* @return this CompletableFuture |
2611 |
jsr166 |
1.182 |
* @since 9 |
2612 |
dl |
1.146 |
*/ |
2613 |
jsr166 |
1.147 |
public CompletableFuture<T> completeOnTimeout(T value, long timeout, |
2614 |
dl |
1.146 |
TimeUnit unit) { |
2615 |
dl |
1.158 |
if (unit == null) |
2616 |
|
|
throw new NullPointerException(); |
2617 |
dl |
1.146 |
if (result == null) |
2618 |
|
|
whenComplete(new Canceller(Delayer.delay( |
2619 |
|
|
new DelayedCompleter<T>(this, value), |
2620 |
|
|
timeout, unit))); |
2621 |
|
|
return this; |
2622 |
|
|
} |
2623 |
|
|
|
2624 |
|
|
/** |
2625 |
jsr166 |
1.174 |
* Returns a new Executor that submits a task to the given base |
2626 |
dl |
1.164 |
* executor after the given delay (or no delay if non-positive). |
2627 |
jsr166 |
1.175 |
* Each delay commences upon invocation of the returned executor's |
2628 |
|
|
* {@code execute} method. |
2629 |
dl |
1.143 |
* |
2630 |
|
|
* @param delay how long to delay, in units of {@code unit} |
2631 |
|
|
* @param unit a {@code TimeUnit} determining how to interpret the |
2632 |
|
|
* {@code delay} parameter |
2633 |
|
|
* @param executor the base executor |
2634 |
|
|
* @return the new delayed executor |
2635 |
jsr166 |
1.182 |
* @since 9 |
2636 |
dl |
1.143 |
*/ |
2637 |
|
|
public static Executor delayedExecutor(long delay, TimeUnit unit, |
2638 |
|
|
Executor executor) { |
2639 |
|
|
if (unit == null || executor == null) |
2640 |
|
|
throw new NullPointerException(); |
2641 |
|
|
return new DelayedExecutor(delay, unit, executor); |
2642 |
|
|
} |
2643 |
|
|
|
2644 |
|
|
/** |
2645 |
|
|
* Returns a new Executor that submits a task to the default |
2646 |
dl |
1.164 |
* executor after the given delay (or no delay if non-positive). |
2647 |
jsr166 |
1.176 |
* Each delay commences upon invocation of the returned executor's |
2648 |
|
|
* {@code execute} method. |
2649 |
dl |
1.143 |
* |
2650 |
|
|
* @param delay how long to delay, in units of {@code unit} |
2651 |
|
|
* @param unit a {@code TimeUnit} determining how to interpret the |
2652 |
|
|
* {@code delay} parameter |
2653 |
|
|
* @return the new delayed executor |
2654 |
jsr166 |
1.182 |
* @since 9 |
2655 |
dl |
1.143 |
*/ |
2656 |
|
|
public static Executor delayedExecutor(long delay, TimeUnit unit) { |
2657 |
jsr166 |
1.163 |
if (unit == null) |
2658 |
|
|
throw new NullPointerException(); |
2659 |
jsr166 |
1.171 |
return new DelayedExecutor(delay, unit, ASYNC_POOL); |
2660 |
dl |
1.143 |
} |
2661 |
|
|
|
2662 |
|
|
/** |
2663 |
|
|
* Returns a new CompletionStage that is already completed with |
2664 |
|
|
* the given value and supports only those methods in |
2665 |
|
|
* interface {@link CompletionStage}. |
2666 |
|
|
* |
2667 |
|
|
* @param value the value |
2668 |
|
|
* @param <U> the type of the value |
2669 |
|
|
* @return the completed CompletionStage |
2670 |
jsr166 |
1.182 |
* @since 9 |
2671 |
dl |
1.143 |
*/ |
2672 |
|
|
public static <U> CompletionStage<U> completedStage(U value) { |
2673 |
|
|
return new MinimalStage<U>((value == null) ? NIL : value); |
2674 |
|
|
} |
2675 |
|
|
|
2676 |
|
|
/** |
2677 |
|
|
* Returns a new CompletableFuture that is already completed |
2678 |
|
|
* exceptionally with the given exception. |
2679 |
|
|
* |
2680 |
jsr166 |
1.151 |
* @param ex the exception |
2681 |
dl |
1.143 |
* @param <U> the type of the value |
2682 |
|
|
* @return the exceptionally completed CompletableFuture |
2683 |
jsr166 |
1.182 |
* @since 9 |
2684 |
dl |
1.143 |
*/ |
2685 |
|
|
public static <U> CompletableFuture<U> failedFuture(Throwable ex) { |
2686 |
|
|
if (ex == null) throw new NullPointerException(); |
2687 |
dl |
1.166 |
return new CompletableFuture<U>(new AltResult(ex)); |
2688 |
dl |
1.143 |
} |
2689 |
|
|
|
2690 |
|
|
/** |
2691 |
|
|
* Returns a new CompletionStage that is already completed |
2692 |
|
|
* exceptionally with the given exception and supports only those |
2693 |
|
|
* methods in interface {@link CompletionStage}. |
2694 |
|
|
* |
2695 |
jsr166 |
1.151 |
* @param ex the exception |
2696 |
dl |
1.143 |
* @param <U> the type of the value |
2697 |
|
|
* @return the exceptionally completed CompletionStage |
2698 |
jsr166 |
1.182 |
* @since 9 |
2699 |
dl |
1.143 |
*/ |
2700 |
dl |
1.153 |
public static <U> CompletionStage<U> failedStage(Throwable ex) { |
2701 |
dl |
1.143 |
if (ex == null) throw new NullPointerException(); |
2702 |
dl |
1.166 |
return new MinimalStage<U>(new AltResult(ex)); |
2703 |
dl |
1.143 |
} |
2704 |
|
|
|
2705 |
|
|
/** |
2706 |
|
|
* Singleton delay scheduler, used only for starting and |
2707 |
|
|
* cancelling tasks. |
2708 |
|
|
*/ |
2709 |
dl |
1.158 |
static final class Delayer { |
2710 |
|
|
static ScheduledFuture<?> delay(Runnable command, long delay, |
2711 |
|
|
TimeUnit unit) { |
2712 |
|
|
return delayer.schedule(command, delay, unit); |
2713 |
|
|
} |
2714 |
|
|
|
2715 |
dl |
1.143 |
static final class DaemonThreadFactory implements ThreadFactory { |
2716 |
|
|
public Thread newThread(Runnable r) { |
2717 |
|
|
Thread t = new Thread(r); |
2718 |
|
|
t.setDaemon(true); |
2719 |
|
|
t.setName("CompletableFutureDelayScheduler"); |
2720 |
|
|
return t; |
2721 |
|
|
} |
2722 |
|
|
} |
2723 |
dl |
1.158 |
|
2724 |
|
|
static final ScheduledThreadPoolExecutor delayer; |
2725 |
|
|
static { |
2726 |
|
|
(delayer = new ScheduledThreadPoolExecutor( |
2727 |
|
|
1, new DaemonThreadFactory())). |
2728 |
|
|
setRemoveOnCancelPolicy(true); |
2729 |
dl |
1.143 |
} |
2730 |
|
|
} |
2731 |
|
|
|
2732 |
|
|
// Little class-ified lambdas to better support monitoring |
2733 |
|
|
|
2734 |
|
|
static final class DelayedExecutor implements Executor { |
2735 |
|
|
final long delay; |
2736 |
|
|
final TimeUnit unit; |
2737 |
|
|
final Executor executor; |
2738 |
|
|
DelayedExecutor(long delay, TimeUnit unit, Executor executor) { |
2739 |
|
|
this.delay = delay; this.unit = unit; this.executor = executor; |
2740 |
|
|
} |
2741 |
|
|
public void execute(Runnable r) { |
2742 |
dl |
1.149 |
Delayer.delay(new TaskSubmitter(executor, r), delay, unit); |
2743 |
dl |
1.143 |
} |
2744 |
|
|
} |
2745 |
|
|
|
2746 |
dl |
1.149 |
/** Action to submit user task */ |
2747 |
|
|
static final class TaskSubmitter implements Runnable { |
2748 |
dl |
1.143 |
final Executor executor; |
2749 |
|
|
final Runnable action; |
2750 |
dl |
1.149 |
TaskSubmitter(Executor executor, Runnable action) { |
2751 |
dl |
1.143 |
this.executor = executor; |
2752 |
|
|
this.action = action; |
2753 |
|
|
} |
2754 |
|
|
public void run() { executor.execute(action); } |
2755 |
|
|
} |
2756 |
|
|
|
2757 |
|
|
/** Action to completeExceptionally on timeout */ |
2758 |
|
|
static final class Timeout implements Runnable { |
2759 |
|
|
final CompletableFuture<?> f; |
2760 |
|
|
Timeout(CompletableFuture<?> f) { this.f = f; } |
2761 |
|
|
public void run() { |
2762 |
|
|
if (f != null && !f.isDone()) |
2763 |
|
|
f.completeExceptionally(new TimeoutException()); |
2764 |
|
|
} |
2765 |
|
|
} |
2766 |
|
|
|
2767 |
dl |
1.149 |
/** Action to complete on timeout */ |
2768 |
dl |
1.146 |
static final class DelayedCompleter<U> implements Runnable { |
2769 |
|
|
final CompletableFuture<U> f; |
2770 |
|
|
final U u; |
2771 |
|
|
DelayedCompleter(CompletableFuture<U> f, U u) { this.f = f; this.u = u; } |
2772 |
dl |
1.166 |
public void run() { |
2773 |
|
|
if (f != null) |
2774 |
|
|
f.complete(u); |
2775 |
|
|
} |
2776 |
dl |
1.146 |
} |
2777 |
|
|
|
2778 |
dl |
1.143 |
/** Action to cancel unneeded timeouts */ |
2779 |
jsr166 |
1.147 |
static final class Canceller implements BiConsumer<Object, Throwable> { |
2780 |
dl |
1.143 |
final Future<?> f; |
2781 |
|
|
Canceller(Future<?> f) { this.f = f; } |
2782 |
|
|
public void accept(Object ignore, Throwable ex) { |
2783 |
|
|
if (ex == null && f != null && !f.isDone()) |
2784 |
|
|
f.cancel(false); |
2785 |
|
|
} |
2786 |
|
|
} |
2787 |
|
|
|
2788 |
jsr166 |
1.168 |
/** |
2789 |
|
|
* A subclass that just throws UOE for most non-CompletionStage methods. |
2790 |
|
|
*/ |
2791 |
dl |
1.143 |
static final class MinimalStage<T> extends CompletableFuture<T> { |
2792 |
|
|
MinimalStage() { } |
2793 |
|
|
MinimalStage(Object r) { super(r); } |
2794 |
jsr166 |
1.168 |
@Override public <U> CompletableFuture<U> newIncompleteFuture() { |
2795 |
dl |
1.143 |
return new MinimalStage<U>(); } |
2796 |
jsr166 |
1.168 |
@Override public T get() { |
2797 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2798 |
jsr166 |
1.168 |
@Override public T get(long timeout, TimeUnit unit) { |
2799 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2800 |
jsr166 |
1.168 |
@Override public T getNow(T valueIfAbsent) { |
2801 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2802 |
jsr166 |
1.168 |
@Override public T join() { |
2803 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2804 |
jsr166 |
1.168 |
@Override public boolean complete(T value) { |
2805 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2806 |
jsr166 |
1.168 |
@Override public boolean completeExceptionally(Throwable ex) { |
2807 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2808 |
jsr166 |
1.168 |
@Override public boolean cancel(boolean mayInterruptIfRunning) { |
2809 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2810 |
jsr166 |
1.168 |
@Override public void obtrudeValue(T value) { |
2811 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2812 |
jsr166 |
1.168 |
@Override public void obtrudeException(Throwable ex) { |
2813 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2814 |
jsr166 |
1.168 |
@Override public boolean isDone() { |
2815 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2816 |
jsr166 |
1.168 |
@Override public boolean isCancelled() { |
2817 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2818 |
jsr166 |
1.168 |
@Override public boolean isCompletedExceptionally() { |
2819 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2820 |
jsr166 |
1.168 |
@Override public int getNumberOfDependents() { |
2821 |
dl |
1.143 |
throw new UnsupportedOperationException(); } |
2822 |
jsr166 |
1.168 |
@Override public CompletableFuture<T> completeAsync |
2823 |
|
|
(Supplier<? extends T> supplier, Executor executor) { |
2824 |
dl |
1.167 |
throw new UnsupportedOperationException(); } |
2825 |
jsr166 |
1.168 |
@Override public CompletableFuture<T> completeAsync |
2826 |
|
|
(Supplier<? extends T> supplier) { |
2827 |
dl |
1.167 |
throw new UnsupportedOperationException(); } |
2828 |
jsr166 |
1.168 |
@Override public CompletableFuture<T> orTimeout |
2829 |
|
|
(long timeout, TimeUnit unit) { |
2830 |
dl |
1.167 |
throw new UnsupportedOperationException(); } |
2831 |
jsr166 |
1.168 |
@Override public CompletableFuture<T> completeOnTimeout |
2832 |
|
|
(T value, long timeout, TimeUnit unit) { |
2833 |
dl |
1.167 |
throw new UnsupportedOperationException(); } |
2834 |
jsr166 |
1.210 |
@Override public CompletableFuture<T> toCompletableFuture() { |
2835 |
|
|
Object r; |
2836 |
|
|
if ((r = result) != null) |
2837 |
|
|
return new CompletableFuture<T>(encodeRelay(r)); |
2838 |
|
|
else { |
2839 |
|
|
CompletableFuture<T> d = new CompletableFuture<>(); |
2840 |
|
|
unipush(new UniRelay<T,T>(d, this)); |
2841 |
|
|
return d; |
2842 |
|
|
} |
2843 |
|
|
} |
2844 |
dl |
1.143 |
} |
2845 |
|
|
|
2846 |
dl |
1.192 |
// VarHandle mechanics |
2847 |
jsr166 |
1.193 |
private static final VarHandle RESULT; |
2848 |
dl |
1.192 |
private static final VarHandle STACK; |
2849 |
|
|
private static final VarHandle NEXT; |
2850 |
dl |
1.1 |
static { |
2851 |
|
|
try { |
2852 |
dl |
1.192 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
2853 |
jsr166 |
1.193 |
RESULT = l.findVarHandle(CompletableFuture.class, "result", Object.class); |
2854 |
dl |
1.192 |
STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class); |
2855 |
|
|
NEXT = l.findVarHandle(Completion.class, "next", Completion.class); |
2856 |
jsr166 |
1.137 |
} catch (ReflectiveOperationException e) { |
2857 |
|
|
throw new Error(e); |
2858 |
dl |
1.1 |
} |
2859 |
jsr166 |
1.159 |
|
2860 |
|
|
// Reduce the risk of rare disastrous classloading in first call to |
2861 |
|
|
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
2862 |
|
|
Class<?> ensureLoaded = LockSupport.class; |
2863 |
dl |
1.1 |
} |
2864 |
|
|
} |