-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureCancelMain.java
More file actions
57 lines (41 loc) · 1.52 KB
/
Copy pathFutureCancelMain.java
File metadata and controls
57 lines (41 loc) · 1.52 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package thread.executor.future;
import util.MyLogger;
import util.ThreadUtils;
import java.util.concurrent.*;
import static util.MyLogger.log;
import static util.ThreadUtils.sleep;
public class FutureCancelMain {
// private static boolean mayInterruterIfRunning = true;
private static boolean mayInterruterIfRunning = false;
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(1);
Future<String> future = es.submit(new MyTask());
sleep(3000);
// MyLogger.log("futuer.state : " + future.state());
//cancel
log("future.cancel(" + mayInterruterIfRunning + ") 호출");
boolean result = future.cancel(mayInterruterIfRunning);
log("future.cancel(" + mayInterruterIfRunning + "). result = " + result);
try {
log("Future result : " + future.get());
} catch (CancellationException e){
log("Future는 이미 취소 되었습니다.");
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
static class MyTask implements Callable<String>{
@Override
public String call() throws Exception {
try {
for (int i = 0; i < 10; i++) {
log("작업중 " + i);
Thread.sleep(1000);
}
}catch (InterruptedException e){
throw new RuntimeException(e);
}
return "Completed";
}
}
}