|
1 |
| -package io.concurrency.chapter11.exam03; |
| 1 | +package chapter11.exam03; |
2 | 2 |
|
3 | 3 | import java.util.concurrent.*;
|
4 | 4 |
|
5 |
| -public class FutureExample { |
| 5 | +public class ExecutorServiceExample { |
6 | 6 |
|
7 | 7 | public static void main(String[] args) throws InterruptedException, ExecutionException {
|
8 | 8 |
|
9 | 9 | ExecutorService executorService = Executors.newFixedThreadPool(2);
|
10 | 10 |
|
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; |
14 | 14 | return result;
|
15 |
| - }; |
| 15 | +// throw new RuntimeException("error");); |
| 16 | + }); |
16 | 17 |
|
17 |
| - Future<Integer> future2 = executorService.submit(task2); |
18 |
| - |
19 |
| - int intermediateResult; |
| 18 | + int result; |
20 | 19 | try {
|
21 |
| - intermediateResult = future2.get(); |
| 20 | + result = future2.get(); |
22 | 21 |
|
23 | 22 | } catch (InterruptedException | ExecutionException e) {
|
24 | 23 | System.out.println("예외 발생: " + e.getMessage());
|
25 |
| - intermediateResult = 0; // 예외가 발생하면 0을 반환 |
| 24 | + result = 0; // 예외가 발생하면 0을 반환 |
26 | 25 | }
|
27 | 26 |
|
28 |
| - int finalResult = intermediateResult; |
| 27 | + int finalResult = result; |
29 | 28 |
|
30 | 29 | System.out.println("최종 결과: " + finalResult);
|
31 | 30 | executorService.shutdown();
|
|
0 commit comments