One of the advantages of the Executor framework is that you can run concurrent tasks that return a result. The Java Concurrency API achieves this with the following two interfaces: Callable: This interface is similar to the Runnable interface. It has…
初识Callable and Future 在编码时,我们可以通过继承Thread或是实现Runnable接口来创建线程,但是这两种方式都存在一个缺陷:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到目的.Java5提供了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果. Callable and Future源码: (1)Callable接口: public interface Callable<V> { V…
一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { public abstract void run(); } 由于run()方法返回值为void类型,所以在执行完任务之后无法返回任何结果. Callable位于java.util.concurrent包下,它也是一个接口,在它里面也只声明了一个方法,只不过这个方法叫做call(): public int…
转载自:http://www.cnblogs.com/dolphin0520/p/3949310.html package future_call; import java.util.concurrent.Callable; /** * Created by luozhitao on 2017/8/10. */ public class Task implements Callable<Integer> { // @Override public Integer call() throws E…