public class Thread4 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i;
System.out.println(Thread.currentThread().getName() + i);
}
return sum;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
Thread4 callable = new Thread4();
FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
Thread thread = new Thread(futureTask, "A线程");
thread.start();
System.out.println(futureTask.get());
}
}
callable创建call,可以通过FutureTask拿到返回值。