Skip to content

Commit 5466d6d

Browse files
committed
lecture
1 parent 5bbe399 commit 5466d6d

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

src/main/java/io/concurrency/chapter11/exam04/RunAsyncExample.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,34 @@
77
public class RunAsyncExample {
88

99
public static void main(String[] args) {
10+
1011
MyService myService = new MyService();
11-
CompletableFuture<Void> voidFuture = CompletableFuture.runAsync(() -> {
12-
// 비동기 작업 시뮬레이션
13-
CompletableFuture<List<Integer>> future = myService.fetchDataAsync();
14-
future.join().stream().forEach(System.out::println);
15-
});
16-
System.out.println("메인 작업 수행 중..");
12+
CompletableFuture.runAsync(() -> {
13+
14+
List<Integer> list = myService.getData();
15+
list.stream().forEach(System.out::println);
1716

18-
voidFuture.join();
17+
}).join();
18+
19+
System.out.println("메인 작업 종료");
1920

2021
}
2122

2223
static class MyService {
2324

24-
public CompletableFuture<List<Integer>> fetchDataAsync() {
25-
26-
return CompletableFuture.supplyAsync(() -> {
27-
// 비동기 작업 시뮬레이션
28-
List<Integer> result = new ArrayList<>();
29-
result.add(1);
30-
result.add(2);
31-
result.add(3);
25+
public List<Integer> getData() {
3226

33-
try {
34-
Thread.sleep(1000); // 비동기 작업 시간 지연 시뮬레이션
35-
} catch (InterruptedException e) {
36-
Thread.currentThread().interrupt();
37-
}
27+
List<Integer> result = new ArrayList<>();
28+
result.add(1);
29+
result.add(2);
30+
result.add(3);
3831

39-
return result;
40-
});
32+
try {
33+
Thread.sleep(1000); // 비동기 작업 시간 지연 시뮬레이션
34+
} catch (InterruptedException e) {
35+
Thread.currentThread().interrupt();
36+
}
37+
return result;
4138
}
4239
}
4340
}

src/main/java/io/concurrency/chapter11/exam04/SupplyAsyncExample.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,33 @@ public class SupplyAsyncExample {
99
public static void main(String[] args) {
1010

1111
MyService myService = new MyService();
12+
CompletableFuture.supplyAsync(() -> {
1213

13-
CompletableFuture<List<Integer>> future = myService.fetchDataAsync();
14-
System.out.println("메인 작업 수행 중..");
15-
future.join().stream().forEach(System.out::println);
14+
List<Integer> list = myService.getData();
15+
list.stream().forEach(System.out::println);
16+
return list;
17+
18+
}).join();
19+
20+
System.out.println("메인 작업 종료");
1621

1722
}
1823

1924
static class MyService {
2025

21-
public CompletableFuture<List<Integer>> fetchDataAsync() {
22-
23-
return CompletableFuture.supplyAsync(() -> {
24-
// 비동기 작업 시뮬레이션
25-
List<Integer> result = new ArrayList<>();
26-
result.add(1);
27-
result.add(2);
28-
result.add(3);
26+
public List<Integer> getData() {
2927

30-
try {
31-
Thread.sleep(1000); // 비동기 작업 시간 지연 시뮬레이션
32-
} catch (InterruptedException e) {
33-
Thread.currentThread().interrupt();
34-
}
28+
List<Integer> result = new ArrayList<>();
29+
result.add(1);
30+
result.add(2);
31+
result.add(3);
3532

36-
return result;
37-
});
33+
try {
34+
Thread.sleep(1000);
35+
} catch (InterruptedException e) {
36+
Thread.currentThread().interrupt();
37+
}
38+
return result;
3839
}
3940
}
4041
}

0 commit comments

Comments
 (0)