-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFuturePatternTest.java
More file actions
42 lines (32 loc) · 1.4 KB
/
FuturePatternTest.java
File metadata and controls
42 lines (32 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.concurrent.*;
public class FuturePatternTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
System.out.println("라면을 끓여봅시다!");
// 1. 물 끓이기 작업을 Future로 비동기 처리
Future<String> boilingWaterFuture = executor.submit(() -> {
System.out.println("물을 끓이는 중...");
Thread.sleep(3000); // 물 끓이는 데 3초
return "물이 끓었습니다!";
});
try {
// 다른 작업을 하다가...
System.out.println("김치 꺼내기, 젓가락 준비 중...");
// 2. 물이 다 끓었는지 확인하고 결과 받기 (blocking)
String result = boilingWaterFuture.get(); // 여기서 기다림 (blocking)
System.out.println(result);
// 3. 물이 끓은 후에 나머지 작업 실행
System.out.println("스프 넣기...");
Thread.sleep(1000);
System.out.println("면 넣기...");
Thread.sleep(1000);
System.out.println("끓이는 중...");
Thread.sleep(1000);
System.out.println("라면 완성!");
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
executor.shutdown();
}
}
}