File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed
src/main/java/io/concurrency/chapter11/exam10 Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change 5
5
public class completeExceptionallyExample {
6
6
public static void main (String [] args ) {
7
7
8
- CompletableFuture <String > cf1 = CompletableFuture . supplyAsync (() -> "Hello World" );
8
+ CompletableFuture <String > cf1 = new CompletableFuture <>( );
9
9
getData (cf1 );
10
-
11
10
CompletableFuture <String > cf2 = cf1
12
11
.thenApply (result -> {
13
12
System .out .println (result );
@@ -21,15 +20,17 @@ public static void main(String[] args) {
21
20
return r ;
22
21
});
23
22
24
-
25
23
System .out .println ("result: " + cf2 .join ());
26
24
}
27
25
28
- static void getData (CompletableFuture <String > future ) {
26
+ private static void getData (CompletableFuture <String > cf1 ) {
29
27
try {
30
- throw new RuntimeException ("error" );
31
- } catch (RuntimeException e ) {
32
- future .completeExceptionally (e );
28
+ System .out .println ("비동기 작업 수행중" );
29
+ Thread .sleep (100 );
30
+ throw new IllegalArgumentException ("error" );
31
+ } catch (Exception e ) {
32
+ cf1 .completeExceptionally (e );
33
33
}
34
+ cf1 .complete ("Hello World" );
34
35
}
35
- }
36
+ }
You can’t perform that action at this time.
0 commit comments