Java并发编程: 并发编程中的ExecutionException异常

一、什么是ExecutionException

在并发编程中在执行java.util.concurrent.Future实现类的get方法时,需要捕获java.util.concurrent.ExecutionException这个异常。Future.get()方法通常是要获取任务的执行结果,当执行任务的过程中抛出了异常,就会产生ExecutionException异常。

二、如何处理ExecutionException

案例分析:

package threadpool;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
public class ExecutorCompletionServiceDemo {

    @Test
    public void TestExecutorCompletionService () throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(50));
        Collection<Callable<String>> tasks = new ArrayList<>();
        tasks.add(() -> {return "1";});
        tasks.add(() -> {return "2";});
        tasks.add(() -> {return "3";});
        tasks.add(() -> {return "4";});
        tasks.add(() -> {return "5";});
        tasks.add(() -> {return "6";});
        tasks.add(() -> {return "7";});
        tasks.add(() -> {return "8";});
        tasks.add(() -> {throw new UnsupportedOperationException("暂不支持该操作");});
        tasks.add(() -> {throw new NullPointerException("NullPointerException occurred");});

        execute(executor, tasks);
    }

    private static void execute(ThreadPoolExecutor executor, Collection<Callable<String>> tasks)  {
        CompletionService<String> completionService = new ExecutorCompletionService<>(executor);
        List<Future<String>> futures = new ArrayList<>(tasks.size());
        for (Callable<String> task : tasks) {
            futures.add(completionService.submit(task));
        }

        for (int i = 0; i < futures.size(); i++) {
            try {
                Future<String> take = completionService.take();
                String s = take.get();
                System.out.println(s);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
               throw new RuntimeException(e);
            }
        }
    }
}

在这里插入图片描述
在execute方法中对于对ExecutionExcetion异常可以做一下处理:
引入依赖:

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
private static void execute(ThreadPoolExecutor executor, Collection<Callable<String>> tasks)  {
        CompletionService<String> completionService = new ExecutorCompletionService<>(executor);
        List<Future<String>> futures = new ArrayList<>(tasks.size());
        for (Callable<String> task : tasks) {
            futures.add(completionService.submit(task));
        }

        for (int i = 0; i < futures.size(); i++) {
            try {
                Future<String> take = completionService.take();
                String s = take.get();
                System.out.println(s);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                Throwable cause = e.getCause() == null ? e : e.getCause();
                Throwables.propagateIfPossible(cause, RuntimeException.class);
                throw new RuntimeException(cause);
            }
        }
    }

在这里插入图片描述
可以看出异常信息清晰了很多,便于开发人员分析。在上面的代码中通过getCause方法获取了来获取真实的异常。接下来使用了Google Guava提供的Throwable类来进行错误的转播与检查,当异常类型为RunTimeException进行抛出,最后使用Throw new RuntimeException(cause)进行兜底。

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉成226

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值