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