-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCallabledAndFutureTest.java
More file actions
38 lines (34 loc) · 991 Bytes
/
Copy pathCallabledAndFutureTest.java
File metadata and controls
38 lines (34 loc) · 991 Bytes
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
package java_base;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class CallableThread implements Callable<Integer>{
private int begin;
private int end;
public CallableThread(int begin, int end) {
// TODO Auto-generated constructor stub
this.begin = begin;
this.end = end;
}
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
int sum = 0;
for(int i = begin; i<=end; i++)
{
sum = sum + i;
}
return sum;
}
}
public class CallabledAndFutureTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Integer> ft1 = es.submit(new CallableThread(1, 100));
Future<Integer> ft2 = es.submit(new CallableThread(101, 200));
System.out.println("1~200 ÕûÊýµÄºÍ:" + (ft1.get() + ft2.get()));
es.shutdown();
}
}