package com.cj.java1; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; //1.创建类实现Callable public class NumThread implements Callable { //实现call public Object call() throws Exception { int sum = 0; for (int i = 0; i <=100; i++) { sum+=i; System.out.println(sum); } return sum; } } class Test1{ public static void main(String[] args) { NumThread numThread = new NumThread(); FutureTask futureTask = new FutureTask(numThread); new Thread(futureTask).start(); try { Object num = futureTask.get(); System.out.println("总和:"+num); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }