-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableMainV2.java
More file actions
39 lines (29 loc) · 1.2 KB
/
Copy pathCallableMainV2.java
File metadata and controls
39 lines (29 loc) · 1.2 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
package thread.executor.future;
import util.MyLogger;
import util.ThreadUtils;
import java.util.Random;
import java.util.concurrent.*;
public class CallableMainV2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService es = Executors.newFixedThreadPool(1);
MyLogger.log("submit() 호출");
Future<Integer> future = es.submit(new MyCallable());
MyLogger.log("future 즉시 반환, future = " + future);
MyLogger.log("future.get() [블로킹] 메서드 호출 시작 -> main 스레드 WATING");
Integer result = future.get();
MyLogger.log("future.get() [블로킹] 메서드 호출 완료 -> main 스레드 RUNNABLE");
MyLogger.log("resule value = " + result);
MyLogger.log("future 완료, future = " + future);
es.shutdownNow();
}
static class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
MyLogger.log("Callable 시작");
ThreadUtils.sleep(2000);
int value = new Random().nextInt(10);
MyLogger.log("Callable 종료");
return value;
}
}
}