@@ -6,13 +6,20 @@ public class ThenApplyExample {
6
6
public static void main (String [] args ) {
7
7
8
8
MyService myService = new MyService ();
9
- CompletableFuture <Integer > asyncFuture = myService .fetchAsyncData ()
9
+ CompletableFuture <Integer > asyncFuture = CompletableFuture .supplyAsync (() -> {
10
+ try {
11
+ Thread .sleep (1000 );
12
+ } catch (InterruptedException e ) {
13
+ Thread .currentThread ().interrupt ();
14
+ }
15
+ return 40 ;
16
+ })
10
17
.thenApply (result -> {
11
- int r = myService .performData2 ( result );
18
+ int r = myService .getData1 ( );
12
19
System .out .println ("thread:" + Thread .currentThread ().getName ());
13
20
return r + result ;
14
21
}).thenApplyAsync (result -> {
15
- int r = myService .performData2 ( result );
22
+ int r = myService .getData2 ( );
16
23
System .out .println ("thread:" + Thread .currentThread ().getName ());
17
24
return r + result ;
18
25
});
@@ -23,22 +30,22 @@ public static void main(String[] args) {
23
30
24
31
static class MyService {
25
32
26
- public CompletableFuture <Integer > fetchAsyncData () {
27
- return CompletableFuture .supplyAsync (() -> {
28
- try {
29
- Thread .sleep (1000 ); // 2초 동안 대기
30
- } catch (InterruptedException e ) {
31
- Thread .currentThread ().interrupt ();
32
- }
33
- return 40 ;
34
- });
33
+ public int getData1 (){
34
+ try {
35
+ Thread .sleep (1000 );
36
+ } catch (InterruptedException e ) {
37
+ throw new RuntimeException (e );
38
+ }
39
+ return 50 ;
35
40
}
36
41
37
- public int performData (int input ) {
38
- return input * 2 ;
39
- }
40
- public int performData2 (int input ) {
41
- return input * 2 ;
42
+ public int getData2 (){
43
+ try {
44
+ Thread .sleep (1000 );
45
+ } catch (InterruptedException e ) {
46
+ throw new RuntimeException (e );
47
+ }
48
+ return 60 ;
42
49
}
43
50
}
44
51
}
0 commit comments