Skip to content

Commit 998eec9

Browse files
committed
lecture
1 parent 5466d6d commit 998eec9

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/main/java/io/concurrency/chapter11/exam03/FutureExample.java renamed to src/main/java/io/concurrency/chapter11/exam03/ExecutorServiceExample.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
package io.concurrency.chapter11.exam03;
1+
package chapter11.exam03;
22

33
import java.util.concurrent.*;
44

5-
public class FutureExample {
5+
public class ExecutorServiceExample {
66

77
public static void main(String[] args) throws InterruptedException, ExecutionException {
88

99
ExecutorService executorService = Executors.newFixedThreadPool(2);
1010

11-
Callable<Integer> task1 = () -> 2;
12-
Callable<Integer> task2 = () -> {
13-
int result = task1.call() + 3;
11+
Future<Integer> future1 = executorService.submit(() -> 2);
12+
Future<Integer> future2 = executorService.submit(() -> {
13+
int result = future1.get() + 3;
1414
return result;
15-
};
15+
// throw new RuntimeException("error"););
16+
});
1617

17-
Future<Integer> future2 = executorService.submit(task2);
18-
19-
int intermediateResult;
18+
int result;
2019
try {
21-
intermediateResult = future2.get();
20+
result = future2.get();
2221

2322
} catch (InterruptedException | ExecutionException e) {
2423
System.out.println("예외 발생: " + e.getMessage());
25-
intermediateResult = 0; // 예외가 발생하면 0을 반환
24+
result = 0; // 예외가 발생하면 0을 반환
2625
}
2726

28-
int finalResult = intermediateResult;
27+
int finalResult = result;
2928

3029
System.out.println("최종 결과: " + finalResult);
3130
executorService.shutdown();

0 commit comments

Comments
 (0)