callable接口与runnable接口的区别之一就是callable接口可以有返回值,并且callable接口提供了泛型来作为返回的类型。callable接口的执行方法是call,区别于runnable接口的run方法:
@FunctionalInterface public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
callable接口提供了返回值,那么在你需要这种情形的条件下就可以使用它,例如我要使用线程池并发执行多个计算任务,各个任务彼此是独立的,最后我想统计各个任务计算结果的总和,那么我使用线程池提交多个callable,如果返回类型是long,那么泛型就要定义为Callable<Long>,注意必须填包装类,不能是基本类型,否则会报type argument can not be of primitive type
当我executorService.submit(new Callable<Long>(){.......})多次以后,可以通过返回的Future<Long>来统计。但有时你又想使用callable接口,又不想使用返回值,那么callable也支持void类型,但是要注意,泛型里面void的v必须大写,这样Callable<Void>;源码如下
/** * The {@code Void} class is an uninstantiable placeholder class to hold a * reference to the {@code Class} object representing the Java keyword * void. * * @author unascribed * @since JDK1.1 */ public final class Void { /** * The {@code Class} object representing the pseudo-type corresponding to * the keyword {@code void}. */ @SuppressWarnings("unchecked") public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void"); /* * The Void class cannot be instantiated. */ private Void() {} }从源码中可以看出Void类型为void的伪类;是一个不能被实例化的占位符,而且当你使用Void作为返回类型时,你发现不写return还报错,因为编译器不会把Void当作void,所以你必须返回点什么,例如null。
executorService.submit(new Callable<Void>() { @Override public Void call() throws Exception { // 业务逻辑 .......... return null; } });
参考地址:https://stackoverflow.com/questions/24381302/callablevoid-as-functional-interface-with-lambdas
https://stackoverflow.com/questions/22795563/how-to-use-callable-with-void-return-type