-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumTaskMainV2.java
More file actions
52 lines (38 loc) · 1.31 KB
/
Copy pathSumTaskMainV2.java
File metadata and controls
52 lines (38 loc) · 1.31 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
package thread.executor.future;
import util.MyLogger;
import java.util.concurrent.*;
import static util.MyLogger.log;
public class SumTaskMainV2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
SumTask task1 = new SumTask(1, 50);
SumTask task2 = new SumTask(51, 100);
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Integer> future1 = es.submit(task1);
Future<Integer> future2 = es.submit(task2);
Integer sum1 = future1.get();
Integer sum2 = future2.get();
log("task1.result = " + sum1);
log("task2.result = " + sum2);
Integer sumAll = sum1 + sum2;
log("sumAll = " + sumAll);
}
static class SumTask implements Callable<Integer> {
int startValue;
int endValue;
public SumTask(int startValue, int endValue) {
this.startValue = startValue;
this.endValue = endValue;
}
@Override
public Integer call() throws Exception {
log("작업 시작");
Thread.sleep(2000);
int sum = 0;
for (int i = startValue; i <= endValue; i++) {
sum += i;
}
log("작업 완료 result = " + sum);
return sum;
}
}
}