标签:pack call() future one stat art integer ring get
package pers.aaa.callable; import java.util.concurrent.Callable; public class MyCallable implements Callable<Integer>{ public Integer call() throws Exception {//call方法可以抛出异常,run不能 int sum=0; for(int i=0;i<=100;i++) sum=sum+i; return sum;//call方法有返回值 } }
package pers.aaa.callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class Test { public static void main(String[] args) throws InterruptedException, ExecutionException { MyCallable mc=new MyCallable(); FutureTask<Integer> result = new FutureTask<Integer>(mc);//创建一个 FutureTask,一旦运行就执行给定的 Callable new Thread(result).start(); Integer sum = result.get(); System.out.println(sum); } }
标签:pack call() future one stat art integer ring get
原文地址:https://www.cnblogs.com/zhai1997/p/11370414.html