Skip to content

Commit 6350805

Browse files
committed
lecture
1 parent b11ea94 commit 6350805

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/main/java/io/concurrency/chapter11/exam05/ThenApplyExample.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@ public class ThenApplyExample {
66
public static void main(String[] args) {
77

88
MyService myService = new MyService();
9+
long started = System.currentTimeMillis();
910
CompletableFuture<Integer> asyncFuture = CompletableFuture.supplyAsync(() -> {
1011
System.out.println("thread1:" + Thread.currentThread().getName());
12+
try {
13+
Thread.sleep(1000);
14+
} catch (InterruptedException e) {
15+
throw new RuntimeException(e);
16+
}
1117
return 40;
1218
})
1319
.thenApply(result -> {
14-
int r = myService.getData1();
1520
System.out.println("thread2:" + Thread.currentThread().getName());
21+
int r = myService.getData1();
1622
return r + result;
17-
}).thenApply(result -> {
18-
int r = myService.getData2();
23+
}).thenApplyAsync(result -> {
1924
System.out.println("thread3:" + Thread.currentThread().getName());
25+
int r = myService.getData2();
2026
return r + result;
2127
});
2228

2329
int asyncResult = asyncFuture.join();
2430
System.out.println("final result: " + asyncResult);
31+
System.out.println("총 소요 시간: " + (System.currentTimeMillis() - started));
2532
}
2633

2734
static class MyService {
2835

2936
public int getData1(){
3037
try {
31-
Thread.sleep(500);
38+
Thread.sleep(1000);
3239
} catch (InterruptedException e) {
3340
throw new RuntimeException(e);
3441
}
@@ -37,7 +44,7 @@ public int getData1(){
3744

3845
public int getData2(){
3946
try {
40-
Thread.sleep(500);
47+
Thread.sleep(1000);
4148
} catch (InterruptedException e) {
4249
throw new RuntimeException(e);
4350
}

0 commit comments

Comments
 (0)