【java并发编程】多线程异常处理

1.场景

​ 多线程充分发挥了系统的性能,但是调用Thread.start()方法之后,如果线程有异常造成线程终止,主线程无法及时获取。

  public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            //todo 添加要执行内容
            for(int i=0;i<10;i++){
                if(i==2){
                    // 模拟出现异常
                    throw new NullPointerException();
                }
            }
            // todo 添加要执行的内容
            System.out.println("执行完成");
        });
        thread.start();
        System.out.println("主线程执行完成");
    }

主线程执行完成
Thread-0抛出异常
Exception in thread “Thread-0” java.lang.NullPointerException
at com.example.demo.ChannelTest.lambda$main$0(ChannelTest.java:18)
at java.lang.Thread.run(Thread.java:748)

​ 如上代码,主线程无法对子线程进行异常的捕获处理。

2.UncaughtExceptionHandler接口

​ JDK1.5之后,为了解决线程run()方法的异常无法捕获的问题,引入了UncaughtExceptionHandler接口,该接口是Thread的内部类定义的。

//当线程抛出异常之后,会调用此方法
void uncaughtException(Thread t, Throwable e);

通过实现UncaughtExceptionHandler接口可以对抛出异常的线程做处理(记录日志),

public class Test {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"抛出异常");
           throw new NullPointerException();
        });
        thread.setUncaughtExceptionHandler(new GetException());
        thread.start();
    }
    static  class GetException implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("异常被捕获");
            System.out.println(t.getName());
            System.out.println(e);
        }
    }
}

输出:

Thread-0抛出异常
异常被捕获
Thread-0
java.lang.NullPointerException

这样,就可以在多线程使用的时候进行异常处理。通过异常处理就可以使程序更加健壮。

猜你喜欢

转载自blog.csdn.net/qq_30285985/article/details/103915533