/
CompletionStage.java
907 lines (841 loc) · 37.6 KB
/
CompletionStage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package java.util.concurrent;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* A stage of a possibly asynchronous computation, that performs an
* action or computes a value when another CompletionStage completes.
* A stage completes upon termination of its computation, but this may
* in turn trigger other dependent stages. The functionality defined
* in this interface takes only a few basic forms, which expand out to
* a larger set of methods to capture a range of usage styles:
*
* <ul>
*
* <li>The computation performed by a stage may be expressed as a
* Function, Consumer, or Runnable (using methods with names including
* <em>apply</em>, <em>accept</em>, or <em>run</em>, respectively)
* depending on whether it requires arguments and/or produces results.
* For example:
* <pre> {@code
* stage.thenApply(x -> square(x))
* .thenAccept(x -> System.out.print(x))
* .thenRun(() -> System.out.println());}</pre>
*
* An additional form (<em>compose</em>) allows the construction of
* computation pipelines from functions returning completion stages.
*
* <p>Any argument to a stage's computation is the outcome of a
* triggering stage's computation.
*
* <li>One stage's execution may be triggered by completion of a
* single stage, or both of two stages, or either of two stages.
* Dependencies on a single stage are arranged using methods with
* prefix <em>then</em>. Those triggered by completion of
* <em>both</em> of two stages may <em>combine</em> their results or
* effects, using correspondingly named methods. Those triggered by
* <em>either</em> of two stages make no guarantees about which of the
* results or effects are used for the dependent stage's computation.
*
* <li>Dependencies among stages control the triggering of
* computations, but do not otherwise guarantee any particular
* ordering. Additionally, execution of a new stage's computations may
* be arranged in any of three ways: default execution, default
* asynchronous execution (using methods with suffix <em>async</em>
* that employ the stage's default asynchronous execution facility),
* or custom (via a supplied {@link Executor}). The execution
* properties of default and async modes are specified by
* CompletionStage implementations, not this interface. Methods with
* explicit Executor arguments may have arbitrary execution
* properties, and might not even support concurrent execution, but
* are arranged for processing in a way that accommodates asynchrony.
*
* <li>Two method forms ({@link #handle handle} and {@link
* #whenComplete whenComplete}) support unconditional computation
* whether the triggering stage completed normally or exceptionally.
* Method {@link #exceptionally exceptionally} supports computation
* only when the triggering stage completes exceptionally, computing a
* replacement result, similarly to the java {@code catch} keyword.
* In all other cases, if a stage's computation terminates abruptly
* with an (unchecked) exception or error, then all dependent stages
* requiring its completion complete exceptionally as well, with a
* {@link CompletionException} holding the exception as its cause. If
* a stage is dependent on <em>both</em> of two stages, and both
* complete exceptionally, then the CompletionException may correspond
* to either one of these exceptions. If a stage is dependent on
* <em>either</em> of two others, and only one of them completes
* exceptionally, no guarantees are made about whether the dependent
* stage completes normally or exceptionally. In the case of method
* {@code whenComplete}, when the supplied action itself encounters an
* exception, then the stage completes exceptionally with this
* exception unless the source stage also completed exceptionally, in
* which case the exceptional completion from the source stage is
* given preference and propagated to the dependent stage.
*
* </ul>
*
* <p>All methods adhere to the above triggering, execution, and
* exceptional completion specifications (which are not repeated in
* individual method specifications). Additionally, while arguments
* used to pass a completion result (that is, for parameters of type
* {@code T}) for methods accepting them may be null, passing a null
* value for any other parameter will result in a {@link
* NullPointerException} being thrown.
*
* <p>Method form {@link #handle handle} is the most general way of
* creating a continuation stage, unconditionally performing a
* computation that is given both the result and exception (if any) of
* the triggering CompletionStage, and computing an arbitrary result.
* Method {@link #whenComplete whenComplete} is similar, but preserves
* the result of the triggering stage instead of computing a new one.
* Because a stage's normal result may be {@code null}, both methods
* should have a computation structured thus:
*
* <pre>{@code (result, exception) -> {
* if (exception == null) {
* // triggering stage completed normally
* } else {
* // triggering stage completed exceptionally
* }
* }}</pre>
*
* <p>This interface does not define methods for initially creating,
* forcibly completing normally or exceptionally, probing completion
* status or results, or awaiting completion of a stage.
* Implementations of CompletionStage may provide means of achieving
* such effects, as appropriate. Method {@link #toCompletableFuture}
* enables interoperability among different implementations of this
* interface by providing a common conversion type.
*
* @author Doug Lea
* @since 1.8
*/
// "阶段"接口
public interface CompletionStage<T> {
/**
* Returns a new CompletionStage that, when this stage completes normally,
* is executed with this stage's result as the argument to the supplied function.
*
* <p>This method is analogous to
* {@link java.util.Optional#map Optional.map} and
* {@link java.util.stream.Stream#map Stream.map}.
*
* <p>See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 同步执行Function任务
<U> CompletionStage<U> thenApply(Function<? super T, ? extends U> fn);
/**
* Returns a new CompletionStage that, when this stage completes normally,
* is executed using this stage's default asynchronous execution facility,
* with this stage's result as the argument to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> thenApplyAsync(Function<? super T, ? extends U> fn);
/**
* Returns a new CompletionStage that, when this stage completes normally,
* is executed using the supplied Executor,
* with this stage's result as the argument to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, is executed with this stage's result as the argument
* to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 同步执行Consumer任务
CompletionStage<Void> thenAccept(Consumer<? super T> action);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, is executed using this stage's default asynchronous
* execution facility, with this stage's result as the argument to
* the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 异步执行Consumer任务
CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, is executed using the supplied Executor, with this
* stage's result as the argument to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行Consumer任务
CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, executes the given action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 同步执行Runnable任务
CompletionStage<Void> thenRun(Runnable action);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, executes the given action using this stage's default
* asynchronous execution facility.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> thenRunAsync(Runnable action);
/**
* Returns a new CompletionStage that, when this stage completes
* normally, executes the given action using the supplied Executor.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> thenRunAsync(Runnable action, Executor executor);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed with the two
* results as arguments to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the type of the other CompletionStage's result
* @param <V> the function's return type
*
* @return the new CompletionStage
*/
// 同步执行BiFunction任务
<U, V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed using this
* stage's default asynchronous execution facility, with the two
* results as arguments to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the type of the other CompletionStage's result
* @param <V> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行BiFunction任务
<U, V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed using the
* supplied executor, with the two results as arguments to the
* supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the type of the other CompletionStage's result
* @param <V> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行BiFunction任务
<U, V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T, ? super U, ? extends V> fn, Executor executor);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed with the two
* results as arguments to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param <U> the type of the other CompletionStage's result
*
* @return the new CompletionStage
*/
// 同步执行BiConsumer任务
<U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed using this
* stage's default asynchronous execution facility, with the two
* results as arguments to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param <U> the type of the other CompletionStage's result
*
* @return the new CompletionStage
*/
// 异步执行BiConsumer任务
<U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, is executed using the
* supplied executor, with the two results as arguments to the
* supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the type of the other CompletionStage's result
*
* @return the new CompletionStage
*/
// 异步执行BiConsumer任务
<U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, executes the given action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 同步执行Runnable任务
CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, executes the given action
* using this stage's default asynchronous execution facility.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);
/**
* Returns a new CompletionStage that, when this and the other
* given stage both complete normally, executes the given action
* using the supplied executor.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed with the
* corresponding result as argument to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 同步执行Function任务
<U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed using this
* stage's default asynchronous execution facility, with the
* corresponding result as argument to the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed using the
* supplied executor, with the corresponding result as argument to
* the supplied function.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed with the
* corresponding result as argument to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 同步执行Consumer任务
CompletionStage<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed using this
* stage's default asynchronous execution facility, with the
* corresponding result as argument to the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 异步执行Consumer任务
CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, is executed using the
* supplied executor, with the corresponding result as argument to
* the supplied action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行Consumer任务
CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, executes the given action.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 同步执行Runnable任务
CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, executes the given action
* using this stage's default asynchronous execution facility.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);
/**
* Returns a new CompletionStage that, when either this or the
* other given stage complete normally, executes the given action
* using the supplied executor.
*
* See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param other the other CompletionStage
* @param action the action to perform before completing the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行Runnable任务
CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor);
/**
* Returns a new CompletionStage that is completed with the same
* value as the CompletionStage returned by the given function.
*
* <p>When this stage completes normally, the given function is
* invoked with this stage's result as the argument, returning
* another CompletionStage. When that stage completes normally,
* the CompletionStage returned by this method is completed with
* the same value.
*
* <p>To ensure progress, the supplied function must arrange
* eventual completion of its result.
*
* <p>This method is analogous to
* {@link java.util.Optional#flatMap Optional.flatMap} and
* {@link java.util.stream.Stream#flatMap Stream.flatMap}.
*
* <p>See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute another CompletionStage
* @param <U> the type of the returned CompletionStage's result
*
* @return the new CompletionStage
*/
// 同步执行Function任务
<U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
/**
* Returns a new CompletionStage that is completed with the same
* value as the CompletionStage returned by the given function,
* executed using this stage's default asynchronous execution
* facility.
*
* <p>When this stage completes normally, the given function is
* invoked with this stage's result as the argument, returning
* another CompletionStage. When that stage completes normally,
* the CompletionStage returned by this method is completed with
* the same value.
*
* <p>To ensure progress, the supplied function must arrange
* eventual completion of its result.
*
* <p>See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute another CompletionStage
* @param <U> the type of the returned CompletionStage's result
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn);
/**
* Returns a new CompletionStage that is completed with the same
* value as the CompletionStage returned by the given function,
* executed using the supplied Executor.
*
* <p>When this stage completes normally, the given function is
* invoked with this stage's result as the argument, returning
* another CompletionStage. When that stage completes normally,
* the CompletionStage returned by this method is completed with
* the same value.
*
* <p>To ensure progress, the supplied function must arrange
* eventual completion of its result.
*
* <p>See the {@link CompletionStage} documentation for rules
* covering exceptional completion.
*
* @param fn the function to use to compute another CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the type of the returned CompletionStage's result
*
* @return the new CompletionStage
*/
// 异步执行Function任务
<U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor);
/**
* Returns a new CompletionStage that, when this stage completes
* either normally or exceptionally, is executed with this stage's
* result and exception as arguments to the supplied function.
*
* <p>When this stage is complete, the given function is invoked
* with the result (or {@code null} if none) and the exception (or
* {@code null} if none) of this stage as arguments, and the
* function's result is used to complete the returned stage.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 同步执行BiFunction任务
<U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
/**
* Returns a new CompletionStage that, when this stage completes
* either normally or exceptionally, is executed using this stage's
* default asynchronous execution facility, with this stage's
* result and exception as arguments to the supplied function.
*
* <p>When this stage is complete, the given function is invoked
* with the result (or {@code null} if none) and the exception (or
* {@code null} if none) of this stage as arguments, and the
* function's result is used to complete the returned stage.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行BiFunction任务
<U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
/**
* Returns a new CompletionStage that, when this stage completes
* either normally or exceptionally, is executed using the
* supplied executor, with this stage's result and exception as
* arguments to the supplied function.
*
* <p>When this stage is complete, the given function is invoked
* with the result (or {@code null} if none) and the exception (or
* {@code null} if none) of this stage as arguments, and the
* function's result is used to complete the returned stage.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
*
* @return the new CompletionStage
*/
// 异步执行BiFunction任务
<U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor);
/**
* Returns a new CompletionStage with the same result or exception as
* this stage, that executes the given action when this stage completes.
*
* <p>When this stage is complete, the given action is invoked
* with the result (or {@code null} if none) and the exception (or
* {@code null} if none) of this stage as arguments. The returned
* stage is completed when the action returns.
*
* <p>Unlike method {@link #handle handle},
* this method is not designed to translate completion outcomes,
* so the supplied action should not throw an exception. However,
* if it does, the following rules apply: if this stage completed
* normally but the supplied action throws an exception, then the
* returned stage completes exceptionally with the supplied
* action's exception. Or, if this stage completed exceptionally
* and the supplied action throws an exception, then the returned
* stage completes exceptionally with this stage's exception.
*
* @param action the action to perform
*
* @return the new CompletionStage
*/
// 同步执行BiConsumer任务
CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
/**
* Returns a new CompletionStage with the same result or exception as
* this stage, that executes the given action using this stage's
* default asynchronous execution facility when this stage completes.
*
* <p>When this stage is complete, the given action is invoked with the
* result (or {@code null} if none) and the exception (or {@code null}
* if none) of this stage as arguments. The returned stage is completed
* when the action returns.
*
* <p>Unlike method {@link #handleAsync(BiFunction) handleAsync},
* this method is not designed to translate completion outcomes,
* so the supplied action should not throw an exception. However,
* if it does, the following rules apply: If this stage completed
* normally but the supplied action throws an exception, then the
* returned stage completes exceptionally with the supplied
* action's exception. Or, if this stage completed exceptionally
* and the supplied action throws an exception, then the returned
* stage completes exceptionally with this stage's exception.
*
* @param action the action to perform
*
* @return the new CompletionStage
*/
// 异步执行BiConsumer任务
CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);
/**
* Returns a new CompletionStage with the same result or exception as
* this stage, that executes the given action using the supplied
* Executor when this stage completes.
*
* <p>When this stage is complete, the given action is invoked with the
* result (or {@code null} if none) and the exception (or {@code null}
* if none) of this stage as arguments. The returned stage is completed
* when the action returns.
*
* <p>Unlike method {@link #handleAsync(BiFunction, Executor) handleAsync},
* this method is not designed to translate completion outcomes,
* so the supplied action should not throw an exception. However,
* if it does, the following rules apply: If this stage completed
* normally but the supplied action throws an exception, then the
* returned stage completes exceptionally with the supplied
* action's exception. Or, if this stage completed exceptionally
* and the supplied action throws an exception, then the returned
* stage completes exceptionally with this stage's exception.
*
* @param action the action to perform
* @param executor the executor to use for asynchronous execution
*
* @return the new CompletionStage
*/
// 异步执行BiConsumer任务
CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor);
/**
* Returns a new CompletionStage that, when this stage completes
* exceptionally, is executed with this stage's exception as the
* argument to the supplied function. Otherwise, if this stage
* completes normally, then the returned stage also completes
* normally with the same value.
*
* @param fn the function to use to compute the value of the
* returned CompletionStage if this CompletionStage completed
* exceptionally
*
* @return the new CompletionStage
*/
// 返回一个新阶段:该阶段会使用当前阶段的执行结果;如果当前阶段产生的是"异常"结果,则会先通过action来处理/转换它
CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn);
/**
* Returns a {@link CompletableFuture} maintaining the same completion properties as this stage.
* If this stage is already a CompletableFuture, this method may return this stage itself.
* Otherwise, invocation of this method may be equivalent in effect to {@code thenApply(x -> x)},
* but returning an instance of type {@code CompletableFuture}.
*
* @return the CompletableFuture
*/
// 对当前阶段进行转换,默认是原样返回
CompletableFuture<T> toCompletableFuture();
}