How is exception handling done in a Callable

前端 未结 2 959
长发绾君心
长发绾君心 2020-12-31 04:24

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 m

2条回答
  •  猫巷女王i
    2020-12-31 04:56

    The point of Callable is to have your exception thrown to your calling thread, for example when you get the result of a Future to which you submitted your callable.

    public class CallableClass implements Callable {
    ...
    }
    
    ExecutorService executor = new ScheduledThreadPoolExecutor(5);
    Future future = executor.submit(callable);
    
    try {
        System.out.println(future.get());
    } catch (Exception e) {
        // do something
    }
    

提交回复
热议问题