在Java中,线程池中工作线程出现异常的时候,默认会把异常往外抛,同时这个工作线程会因为异常而销毁,我们需要自己去处理对应的异常,异常处理的方法有几种:
-
在传递的任务中去处理异常,对于每个提交到线程池中的执行的任务,可以提前通过异常进行捕获,这样即便出现了异常,也不会影响线程池中的工作线程
-
使用Future来捕获异常结果,在线程池中提供了一个submit(Callable<T>)方法,这个方法会返回一个Future,可以通过调用Future.get()方法,来获取任务的执行结果,如果任务执行过程中出现了异常,也会抛出一个ExecutionException,其中就包含了任务执行过程中出现的异常
-
我们还可以自定义一个ThreadFactory,设置一个UncaughtExceptionHandler,我们可以通过实现ThreadFactory的接口来自定义创建线程的方式,然后为每个新创建的线程设置一个UncaughtExceptionHandler,这个处理器会在线程由于未捕获异常而即将终止的时候被调用
下面是三段代码示例:
捕获线程执行异常
- ExecutorService executorService = Executors.newFixedThreadPool(5);
-
- executorService.execute(() -> {
- try {
- // 执行任务
- } catch (Exception e) {
- // 记录日志
- logger.error("An exception occurred: ", e);
- }
- });
在执行任务的过程中,如果出现异常,就会被try-catch语句捕获,并将异常信息记录到日志中。
使用Future来捕获异常结果
- ExecutorService executorService = Executors.newFixedThreadPool(5);
-
- List<Future<?>> futures = new ArrayList<>();
-
- // 提交任务到线程池
- for (int i = 0; i < 10; i++) {
- Future<?> future = executorService.submit(() -> {
- // 执行任务
- });
- futures.add(future);
- }
-
- // 获取任务结果
- for (Future<?> future : futures) {
- try {
- future.get();
- } catch (InterruptedException | ExecutionException e) {
- // 处理异常
- logger.error("An exception occurred: ", e);
- }
- }
在这个例子中,我们将多个任务提交到线程池,并将每个任务的Future对象保存在futures列表中。接着,我们遍历futures列表,并调用每个Future对象的get()方法来获取任务的执行结果。如果任务执行过程中出现了异常,则会抛出ExecutionException异常,我们在catch块中捕获该异常并进行相应的处理。
实现UncaughtExceptionHandler接口
- public class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
- @Override
- public void uncaughtException(Thread t, Throwable e) {
- // 记录日志或者进行其它处理
- logger.error("Thread " + t.getName() + " encountered an exception: ", e);
- }
- }
-
- ExecutorService executorService = Executors.newFixedThreadPool(5);
-
- // 设置UncaughtExceptionHandler
- ((ThreadPoolExecutor) executorService).setThreadFactory(r -> {
- Thread thread = new Thread(r);
- thread.setUncaughtExceptionHandler(new CustomExceptionHandler());
- return thread;
- });
-
- executorService.execute(() -> {
- // 执行任务
- });
这种方式需要自定义一个实现了UncaughtExceptionHandler接口的异常处理器,当线程出现异常时,异常处理器会被调用,我们可以在其中记录日志或者进行其它处理。接着,我们需要将异常处理器设置到线程池的线程工厂中。当线程池内的线程出现异常时,异常处理器就会被调用,我们可以在其中处理异常。