zoukankan      html  css  js  c++  java
  • java高并发编程--04--Hook线程以及捕获线程执行异常

    1.获取线程运行时异常
    Thread类处理运行时异常的四个API:
    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh):为某个线程UncaughtExceptionHandler
    public static setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh):设置全局UncaughtExceptionHandler
    public UncaughtExceptionHandler getUncaughtExceptionHandler():获取特定线程的UncaughtExceptionHandler
    public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler():获取全局UncaughtExcepitonHandler

    1.UncaughtExceptionHandler简介
    线程在执行单元中不允许抛出checked异常,线程运行在自己的上下文中,派生它的线程无法直接获取到它运行的异常信息,对此,java提供了一个UncaughtExceptionHandler接口,当线程在运行过程中出现异常时会调用UncaughtExceptionHandler接口,从而得知那个线程在运行时出错以及出现什么样的错误。
    UncaughtExceptionHandler接口是Thread类的一个内部接口:

    @FunctionalInterface
    public interface UncaughtExceptionHandler {
        /**
         * Method invoked when the given thread terminates due to the
         * given uncaught exception.
         * <p>Any exception thrown by this method will be ignored by the
         * Java Virtual Machine.
         * @param t the thread
         * @param e the exception
         */
        void uncaughtException(Thread t, Throwable e);
    }

    该接口是一个函数式接口,只有一个方法,会被Thread类的dispatchUncaughtException方法调用:

    /**
     * Dispatch an uncaught exception to the handler. This method is
     * intended to be called only by the JVM.
     */
    private void dispatchUncaughtException(Throwable e) {
        getUncaughtExceptionHandler().uncaughtException(this, e);
    }

    当线程出现异常时,会调用dispatchUncaughtException方法,将对应的线程实例及异常信息传递给回调接口
    使用代码示例如下:

    public class UncaughtExceptionHandlerTest {
        public static void main(String[] args) {
            //设置回调接口实现
            Thread.setDefaultUncaughtExceptionHandler((t,e)->{
                System.out.println("--->线程"+t.getName()+"发生异常,异常信息如下:");
                e.printStackTrace();
            });
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                //这里将抛出unchecked异常
                System.out.println(2/0);
            }).start();
        }
    }

    输出结果:

    --->线程Thread-0发生异常,异常信息如下:
    java.lang.ArithmeticException: / by zero
        at cp7.cp1.UncaughtExceptionHandlerTest.lambda$1(UncaughtExceptionHandlerTest.java:19)
        at java.base/java.lang.Thread.run(Thread.java:834)

    2.Hook线程
    2.1Hook线程简介
    向jvm程序注入一个Hook线程,在jvm程序退出时,Hook线程会启动执行,可以通过Runtime向jvm程序注入多个Hook线程:

    public class HookTest {
        public static void main(String[] args) {
            //注入5个Hook线程
            for(int i = 0;i < 5;i ++) {
                int x = i;
                Runtime.getRuntime().addShutdownHook(new Thread(()->{
                    System.out.println("hook" + x + " run");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("hook" + x + " is over");
                }));
            }
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("main is over");
        }
    }


    输出结果:
    main is over
    hook2 run
    hook1 run
    hook0 run
    hook4 run
    hook3 run
    hook1 is over
    hook4 is over
    hook0 is over
    hook3 is over
    hook2 is over
    2.2Hook线程使用与注意事项
    Hook线程可以阻止程序重复启动,在进程启动时创建一个look文件,创建前先判断这个文件释放存在,如果存在则认为已经启动,程序结束时利用Hook线程的特点删除这个文件。如MySQL、zookeeper、kafka等软件都可以看到lock文件的存在
    注意:
    1)Hook线程只有在收到退出信号时执行,如果使用kill -9 命令结束进程,Hook线程不会执行,lock文件不会被清理
    2)Hook线程可以做一些资源释放工作,如关闭文件句柄、socket链接、数据库connection等
    3)尽量不要在Hook线程中执行一些耗时非常长的操作,否则程序迟迟无法退出

  • 相关阅读:
    iOS_03_为什么选择ios开发
    iOS_02_什么是ios开发
    iOS_01_什么是ios
    Hadoop之HDFS
    hadoop组件及其作用
    数组
    Scala基础知识(二)
    hadoop安装过程
    Scala基础知识
    建造者模式
  • 原文地址:https://www.cnblogs.com/ShouWangYiXin/p/11449594.html
Copyright © 2011-2022 走看看