Skip to content

Commit bcc67ec

Browse files
committed
lecture
1 parent 9ae36c7 commit bcc67ec

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

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

+24-17
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ public class ThenApplyExample {
66
public static void main(String[] args) {
77

88
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+
})
1017
.thenApply(result -> {
11-
int r = myService.performData2(result);
18+
int r = myService.getData1();
1219
System.out.println("thread:" + Thread.currentThread().getName());
1320
return r + result;
1421
}).thenApplyAsync(result -> {
15-
int r = myService.performData2(result);
22+
int r = myService.getData2();
1623
System.out.println("thread:" + Thread.currentThread().getName());
1724
return r + result;
1825
});
@@ -23,22 +30,22 @@ public static void main(String[] args) {
2330

2431
static class MyService {
2532

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;
3540
}
3641

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;
4249
}
4350
}
4451
}

0 commit comments

Comments
 (0)