@@ -6,29 +6,36 @@ public class ThenApplyExample {
6
6
public static void main (String [] args ) {
7
7
8
8
MyService myService = new MyService ();
9
+ long started = System .currentTimeMillis ();
9
10
CompletableFuture <Integer > asyncFuture = CompletableFuture .supplyAsync (() -> {
10
11
System .out .println ("thread1:" + Thread .currentThread ().getName ());
12
+ try {
13
+ Thread .sleep (1000 );
14
+ } catch (InterruptedException e ) {
15
+ throw new RuntimeException (e );
16
+ }
11
17
return 40 ;
12
18
})
13
19
.thenApply (result -> {
14
- int r = myService .getData1 ();
15
20
System .out .println ("thread2:" + Thread .currentThread ().getName ());
21
+ int r = myService .getData1 ();
16
22
return r + result ;
17
- }).thenApply (result -> {
18
- int r = myService .getData2 ();
23
+ }).thenApplyAsync (result -> {
19
24
System .out .println ("thread3:" + Thread .currentThread ().getName ());
25
+ int r = myService .getData2 ();
20
26
return r + result ;
21
27
});
22
28
23
29
int asyncResult = asyncFuture .join ();
24
30
System .out .println ("final result: " + asyncResult );
31
+ System .out .println ("총 소요 시간: " + (System .currentTimeMillis () - started ));
25
32
}
26
33
27
34
static class MyService {
28
35
29
36
public int getData1 (){
30
37
try {
31
- Thread .sleep (500 );
38
+ Thread .sleep (1000 );
32
39
} catch (InterruptedException e ) {
33
40
throw new RuntimeException (e );
34
41
}
@@ -37,7 +44,7 @@ public int getData1(){
37
44
38
45
public int getData2 (){
39
46
try {
40
- Thread .sleep (500 );
47
+ Thread .sleep (1000 );
41
48
} catch (InterruptedException e ) {
42
49
throw new RuntimeException (e );
43
50
}
0 commit comments