如何在Callable中完成异常处理 [英] How is exception handling done in a Callable

查看:346
本文介绍了如何在Callable中完成异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解callable的调用可以将异常抛给调用它的父方法,而runnable不是这样。

I understand that callable's call can throw the exception to the parent method calling it which is not the case with runnable.

我想知道怎么做因为它是一个线程方法而且是线程堆栈的最底层方法。

I wonder how because it's a thread method and is the bottommost method of the thread stack.

推荐答案

Callable 将您的异常抛出到您的调用线程,例如你得到你提交 callable Future 的结果。

public class CallableClass implements Callable<String> {
...
}

ExecutorService executor = new ScheduledThreadPoolExecutor(5);
Future<Integer> future = executor.submit(callable);

try {
    System.out.println(future.get());
} catch (Exception e) {
    // do something
}

这篇关于如何在Callable中完成异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆