Skip to content

Commit af86cab

Browse files
committed
lecture
1 parent a7dcf4f commit af86cab

File tree

4 files changed

+64
-130
lines changed

4 files changed

+64
-130
lines changed

src/main/java/io/concurrency/chapter11/exam06/CompletablePipelineExample.java

-53
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
package io.concurrency.chapter11.exam06;
22

3-
import java.util.ArrayList;
3+
import java.util.Arrays;
44
import java.util.List;
55
import java.util.concurrent.CompletableFuture;
66

77
public class ThenAcceptExample {
8+
89
public static void main(String[] args) {
910

1011
MyService myService = new MyService();
11-
myService.fetchAsyncData()
12-
.thenApply(result -> {
13-
System.out.println(result);
14-
return result.stream().map(i -> i * 2).toList();
12+
CompletableFuture.supplyAsync(() -> {
13+
System.out.println("thread1:" + Thread.currentThread().getName());
14+
try {
15+
Thread.sleep(500);
16+
} catch (InterruptedException e) {
17+
throw new RuntimeException(e);
18+
}
19+
return 40;
20+
})
21+
.thenAccept(result -> {
22+
System.out.println("thread2:" + Thread.currentThread().getName());
23+
List<Integer> r = myService.getData1();
24+
r.forEach(System.out::println);
25+
1526
}).thenAcceptAsync(result -> {
16-
result.stream().forEach(System.out::println);
17-
}).join();
27+
System.out.println("thread3:" + Thread.currentThread().getName());
28+
List<Integer> r = myService.getData2();
29+
r.forEach(System.out::println);
1830

31+
}).join();
1932
}
2033

2134
static class MyService {
2235

23-
public CompletableFuture<List<Integer>> fetchAsyncData() {
24-
return CompletableFuture.supplyAsync(() -> {
25-
26-
List<Integer> result = new ArrayList<>();
27-
result.add(1);
28-
result.add(2);
29-
result.add(3);
30-
31-
try {
32-
Thread.sleep(500); // 비동기 작업 시간 지연 시뮬레이션
33-
} catch (InterruptedException e) {
34-
Thread.currentThread().interrupt();
35-
}
36+
public List<Integer> getData1(){
37+
try {
38+
Thread.sleep(500);
39+
} catch (InterruptedException e) {
40+
throw new RuntimeException(e);
41+
}
42+
return Arrays.asList(1,2,3);
43+
}
3644

37-
return result;
38-
});
45+
public List<Integer> getData2(){
46+
try {
47+
Thread.sleep(500);
48+
} catch (InterruptedException e) {
49+
throw new RuntimeException(e);
50+
}
51+
return Arrays.asList(4,5,6);
3952
}
4053
}
4154
}
55+
56+

src/main/java/io/concurrency/chapter11/exam06/ThenAcceptThreadExample.java

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package io.concurrency.chapter11.exam06;
22

3-
import java.util.ArrayList;
3+
import java.util.Arrays;
44
import java.util.List;
55
import java.util.concurrent.CompletableFuture;
66

77
public class ThenRunExample {
88
public static void main(String[] args) {
99

1010
MyService myService = new MyService();
11-
CompletableFuture<List<Integer>> asyncTask1 = myService.fetchAsyncData()
11+
CompletableFuture<List<Integer>> asyncTask1 = CompletableFuture.supplyAsync(() -> {
12+
System.out.println("thread1:" + Thread.currentThread().getName());
13+
try {
14+
Thread.sleep(500);
15+
} catch (InterruptedException e) {
16+
throw new RuntimeException(e);
17+
}
18+
return 40;
19+
})
1220
.thenApply(result -> {
13-
return result.stream().map(i -> i * 2).toList();
21+
System.out.println("thread2:" + Thread.currentThread().getName());
22+
return myService.getData1();
1423
});
1524

1625
// 비동기 작업 2: 로깅
@@ -32,22 +41,22 @@ public static void main(String[] args) {
3241

3342
static class MyService {
3443

35-
public CompletableFuture<List<Integer>> fetchAsyncData() {
36-
return CompletableFuture.supplyAsync(() -> {
37-
38-
List<Integer> result = new ArrayList<>();
39-
result.add(1);
40-
result.add(2);
41-
result.add(3);
42-
43-
try {
44-
Thread.sleep(500); // 비동기 작업 시간 지연 시뮬레이션
45-
} catch (InterruptedException e) {
46-
Thread.currentThread().interrupt();
47-
}
44+
public List<Integer> getData1(){
45+
try {
46+
Thread.sleep(500);
47+
} catch (InterruptedException e) {
48+
throw new RuntimeException(e);
49+
}
50+
return Arrays.asList(1,2,3);
51+
}
4852

49-
return result;
50-
});
53+
public List<Integer> getData2(){
54+
try {
55+
Thread.sleep(500);
56+
} catch (InterruptedException e) {
57+
throw new RuntimeException(e);
58+
}
59+
return Arrays.asList(4,5,6);
5160
}
5261
}
5362
}

0 commit comments

Comments
 (0)