Callable
Doug Lea 大师,在1.5的杰作。
@FunctionalInterface
public interface Callable<V> {
V call() throws Exception;
}
Callable 是一个泛型接口,里面只有一个 call() 方法,该方法可以返回泛型值 V ,使用起来就像这样:
Callable<String> callable = () -> {
Thread.sleep(2000);
return "Return some result";
};
Runnable VS Callable
执行机制
- 从执行机制上来看,Runnable 你太清楚了,它既可以用在 Thread 类中,也可以用在 ExecutorService 类中配合线程池的使用;
- Callable 只能在 ExecutorService 中使用。
异常处理
Runnable 接口中的 run 方法签名上没有 throws ,自然也就没办法向上传播受检异常;而 Callable 的 call() 方法签名却有 throws,所以它可以处理受检异常;
ExecutorService 中 Runable 和 Callable 的使用
void execute(Runnable command);
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
ExecutorService 的 execute() 方法依旧得不到返回值,而 submit() 方法的返回 Future 类型的返回值
- Future 到底是什么呢?
- 怎么通过它获取返回值呢?
boolean cancel(boolean mayInterruptIfRunning);
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
boolean isCancelled();
boolean isDone();
@Slf4j
public class FutureAndCallableExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> callable = () -> {
log.info("进入 Callable 的 call 方法");
Thread.sleep(5000);
return "Hello from Callable";
};
log.info("提交 Callable 到线程池");
Future<String> future = executorService.submit(callable);
log.info("主线程继续执行");
log.info("主线程等待获取 Future 结果");
String result = future.get();
log.info("主线程获取到 Future 结果: {}", result);
executorService.shutdown();
}
}
使用 Future 方法提供的 isDone 方法,它可以用来检查 task 是否已经完成了
子任务长时间未被处理,则取消子任务。
在这里超过一秒就被取消
调用 get() 方法时,如果计算结果被取消了,则抛出 CancellationException 。
更好的处理异常,增加逻辑判断。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)