public class MyCallable implements Callable<Integer> {
// 由于这里需要2个数,开始和结束的数值。
// 所以创建对象,并且写构造方法接收数值
private int start;
private int end;
public MyCallable(int start, int end) {
this.start = start;
this.end = end;
}
int sum = 0;
public Integer call() throws Exception {
for (int x = start; x <= end; x++) {
sum += x;
}
return sum;
}
}
``
测试类
```java
public class CallableDemo {
public static void main(String[] args) throws InterruptedException,
ExecutionException {
// 创建线程池对象
ExecutorService pool = Executors.newFixedThreadPool(3);