JAVA多线程(六)之Hook 线程以及获取线程执行异常

Hook 线程 简介

Hook线程也叫钩子线程,通常我们在运行的时候向应用程序注入一个或者多个Hook(钩子)线程,这样,JVM在退出的时候,Hook线程将要会启动

获取线程运行时异常
public void setUncaughtExceptionHandle(UncaughtExceptionHandler er);//为某个特定线程指定UncaughtException
public static void setDefaultUncaughtExceptionHandler(UncaughtException eh);//为当前线程设置全局的UncaughtException
public UncaughtExceptionHandler getUncaughtExceptionHandler();//获取特定线程的UncaughtExceptionHandler
public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler();//获取全局的UncaughtExceptionHandler。

方法介绍:
线程在执行单元中是不允许抛出checke异常的,而且线程运行在自己的上下问环境中,派生他的线程将无法直接获取他的异常信息。因此我们需要UncaughtExceptionHandler接口,当线程在运行的过程中出现了异常时,会回调UncaughtExceptionHandler接口,从而得知那个线程在运行时出错。

实例代码如下

    public interface UncaughtExceptionHandler {
        void uncaughtException(Thread t, Throwable e);
    }

从上述代码中,可以看出,UncaughtExceptionHandler 是一个接口,只有一个抽象方法,该接口会被Thread中的dispatchUncaughtException 方法处理回调

  private void dispatchUncaughtException(Throwable e) {
        getUncaughtExceptionHandler().uncaughtException(this, e);
    }

当线程在运行时出现异常的时候,JVM会调用dispatchUncaughtException方法,该接口会讲当前线程和异常信息传递给该接口.。

public class CauptThreadException {
    public static void main(String[] args) {
        Thread thread = new Thread("CaptureThreadException"){
            @Override
            public void run() {
                int i = 1 / 0;
            }
        };
        thread.setDefaultUncaughtExceptionHandler(new MyCaughtExceptionHandler());
        thread.start();
    }
}

public class MyCaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println(t.getName());
        e.printStackTrace();
    }
}

原理解析

我们可以打开Thread 类,看下dispatchUncaughtException 方法 调用了 getUncaughtExceptionHandler() 方法

  public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return uncaughtExceptionHandler != null ?
            uncaughtExceptionHandler : group;
    }

从上述代码我们可以分析 ,首先 获取当前线程中是否有uncaughtExceptionHandler 如果不等于null 则直接返回,否则返回 ThreadGroup。然后根据返回的类型 调用uncaughtException()方法,

 public void uncaughtException(Thread t, Throwable e) {
        if (parent != null) { //首先判断当前Thread是否有父Group ,有则调用父group的方法
            parent.uncaughtException(t, e);
        } else {
           Thread.UncaughtExceptionHandler ueh = Thread.getDefaultUncaughtExceptionHandler();//获取全局的UncaughtException
            if (ueh != null) {
                ueh.uncaughtException(t, e);
            } else if (!(e instanceof ThreadDeath)) { //如果没有父ThreadGroup 同时 也没有全局的uncaughtExceptionHandler 则直接从异常中信息打印到堆栈中
                System.err.print("Exception in thread \""
                                 + t.getName() + "\" ");
                e.printStackTrace(System.err);
            }
        }
    }
JAVA多线程(六)之Hook 线程以及获取线程执行异常_第1张图片
获取uncaughtException 接口流程图

Hook线程应用场景及其注意事项

1.Hook线程只有在收到退出信号才会被执行,如果 在Kill 命令的时候使用了参数 -9 那么hook线程得不到执行。进程也会被退出。
2.Hook线程可以做一些资源释放的工作,比如关闭文件,释放链接等等。
3.不要使用Hook线程执行一些耗时非常长的工作,因为程序迟迟不会退出。

你可能感兴趣的:(JAVA多线程(六)之Hook 线程以及获取线程执行异常)