JUC并发编程 -- 正确处理线程池异常

1. 正确处理线程池异常


1.1 如果不处理异常

示例代码:

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "c.TestTimer")
public class TestTimer {
     
    public static void main(String[] args) throws ExecutionException, InterruptedException {
     
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.schedule(() -> {
     
            log.debug("task1");
            // 主动写一个异常
            int i = 1 / 0;
        }, 1, TimeUnit.SECONDS);

        pool.schedule(() -> {
     
            log.debug("task2");
        }, 1, TimeUnit.SECONDS);

        pool.schedule(() -> {
     
            log.debug("task3");
        }, 1, TimeUnit.SECONDS);
    }
}

运行结果:

程序不会抛出任何的异常信息,直接把里面的任务执行完了,我们都不知道里面有没有发生异常。这样显然是不好的,下面介绍2种捕获异常的方式
JUC并发编程 -- 正确处理线程池异常_第1张图片


1.2 try / catch 捕获异常

示例代码:

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j(topic = "c.TestTimer")
public class TestTimer {
     
    public static void main(String[] args) throws ExecutionException, InterruptedException {
     
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.schedule(() -> {
     
        // 主动捕获异常
            try {
     
                log.debug("task1");
                // 主动写一个异常
                int i = 1 / 0;
            } catch (Exception e) {
     
                log.debug("error: {}", e);
            }
        }, 1, TimeUnit.SECONDS);

        pool.schedule(() -> {
     
            log.debug("task2");
        }, 1, TimeUnit.SECONDS);

        pool.schedule(() -> {
     
            log.debug("task3");
        }, 1, TimeUnit.SECONDS);
    }
}

运行结果:

JUC并发编程 -- 正确处理线程池异常_第2张图片


1.3 使用 Future

示例代码:

package com.tian;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j(topic = "c.TestTimer")
public class TestTimer {
     
    public static void main(String[] args) throws ExecutionException, InterruptedException {
     

        ExecutorService pool = Executors.newFixedThreadPool(1);
        Future<Boolean> f = pool.submit(() -> {
     
            log.debug("task1");
            // 主动抛出异常
            int i = 1 / 0;
            return true;
        });
        // 如果任务里面发生了异常 则 get方法会返回异常信息
        log.debug("result:{}", f.get());
    }
}

运行结果:

在这里插入图片描述



你可能感兴趣的:(JUC--黑马,java,JUC,线程池异常捕获)