七、JAVA多线程:Hook线程以及捕获线程执行异常(UncaughtExceptionHandler、Hook)

        本章将介绍,如何获取线程在运行时期的异常,以及如何向JAVA程序注入Hook线程。

获取线程运行时异常

在Thread类中,关于处理运行时异常的API一共有四个。

1.为某个特定线程指定UncaughtExceptionHandler

public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

2.设置全局的UncaughtExceptionHandler

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

3.获取特定线程的UncaughtExceptionHandler

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

4.获取全局的UncaughtExceptionHandler

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

UncaughtExceptionHandler 介绍

线程在执行单元中不允许抛出checked异常,而且线程运行在自己的上下文中,派生它的线程无法直接获得它运行中出现的异常信息。对此,Java为我们提供了UncaughtExceptionHandler接口,当线程在运行过程中出现异常时,会回调UncaughtExceptionHandler接口,从而得知是哪个线程在运行时出错。UncaughtExceptionHandler接口在Thread中定义。

/**
 * Interface for handlers invoked when a <tt>Thread</tt> abruptly
 * terminates due to an uncaught exception.
 * <p>When a thread is about to terminate due to an uncaught exception
 * the Java Virtual Machine will query the thread for its
 * <tt>UncaughtExceptionHandler</tt> using
 * {@link #getUncaughtExceptionHandler} and will invoke the handler's
 * <tt>uncaughtException</tt> method, passing the thread and the
 * exception as arguments.
 * If a thread has not had its <tt>UncaughtExceptionHandler</tt>
 * explicitly set, then its <tt>ThreadGroup</tt> object acts as its
 * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object
 * has no
 * special requirements for dealing with the exception, it can forward
 * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler
 * default uncaught exception handler}.
 *
 * @see #setDefaultUncaughtExceptionHandler
 * @see #setUncaughtExceptionHandler
 * @see ThreadGroup#uncaughtException
 * @since 1.5
 */
@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);
}

UncaughtExceptionHandler是一个FunctionalInterface ,只有一个抽象方法,

该回调接口会被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);
}

当线程在运行过程中出现异常时,JVM会调用dispatchUncaughtException方法,

该方法会将对应的线程实例以及异常信息传递给回调接口

UncaughtExceptionHandler样例

package com.zl.step7;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.concurrent.TimeUnit;

public class CaptureThreadExeception {
    public static void main(String[] args) {
        Thread.setDefaultUncaughtExceptionHandler((t,e) -> {
            System.out.println(t.getName() + " occur exception");

            e.printStackTrace();

        });

        // 这里将会出现unchecked异常
        final Thread thread = new Thread(() -> {

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            System.out.println(1/0);

        }, "Test-Thread ") ;

        thread.start();

    }
}

返回异常:

Connected to the target VM, address: '127.0.0.1:60373', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:60373', transport: 'socket'
Test-Thread  occur exception
java.lang.ArithmeticException: / by zero
    at com.zl.step7.CaptureThreadExeception.lambda$main$1(CaptureThreadExeception.java:28)
    at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0
 

UncaughtExceptionHandler源码分析

/**
 * Returns the handler invoked when this thread abruptly terminates
 * due to an uncaught exception. If this thread has not had an
 * uncaught exception handler explicitly set then this thread's
 * <tt>ThreadGroup</tt> object is returned, unless this thread
 * has terminated, in which case <tt>null</tt> is returned.
 * @since 1.5
 * @return the uncaught exception handler for this thread
 */
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
    return uncaughtExceptionHandler != null ?
        uncaughtExceptionHandler : group;
}

getUncaughtExceptionHandler 方法首先会判断当前线程是否设置了handler,如果有则执行自己的uncaughtExceptionHandler

否者就到ThreadGroup中获取。

ThreadGroup中的uncaughtExceptionHandler 方法:

/**
 * Called by the Java Virtual Machine when a thread in this
 * thread group stops because of an uncaught exception, and the thread
 * does not have a specific {@link Thread.UncaughtExceptionHandler}
 * installed.
 * <p>
 * The <code>uncaughtException</code> method of
 * <code>ThreadGroup</code> does the following:
 * <ul>
 * <li>If this thread group has a parent thread group, the
 *     <code>uncaughtException</code> method of that parent is called
 *     with the same two arguments.
 * <li>Otherwise, this method checks to see if there is a
 *     {@linkplain Thread#getDefaultUncaughtExceptionHandler default
 *     uncaught exception handler} installed, and if so, its
 *     <code>uncaughtException</code> method is called with the same
 *     two arguments.
 * <li>Otherwise, this method determines if the <code>Throwable</code>
 *     argument is an instance of {@link ThreadDeath}. If so, nothing
 *     special is done. Otherwise, a message containing the
 *     thread's name, as returned from the thread's {@link
 *     Thread#getName getName} method, and a stack backtrace,
 *     using the <code>Throwable</code>'s {@link
 *     Throwable#printStackTrace printStackTrace} method, is
 *     printed to the {@linkplain System#err standard error stream}.
 * </ul>
 * <p>
 * Applications can override this method in subclasses of
 * <code>ThreadGroup</code> to provide alternative handling of
 * uncaught exceptions.
 *
 * @param   t   the thread that is about to exit.
 * @param   e   the uncaught exception.
 * @since   JDK1.0
 */
public void uncaughtException(Thread t, Throwable e) {
    if (parent != null) {
        parent.uncaughtException(t, e);
    } else {
        Thread.UncaughtExceptionHandler ueh =
            Thread.getDefaultUncaughtExceptionHandler();
        if (ueh != null) {
            ueh.uncaughtException(t, e);
        } else if (!(e instanceof ThreadDeath)) {
            System.err.print("Exception in thread \""
                             + t.getName() + "\" ");
            e.printStackTrace(System.err);
        }
    }
}

总结:

1.如果ThreadGroup 如果有父ThreadGroup ,则直接调用父Group的uncaughtException方法

2.如果设置了全局默认UncaughtExceptionHandler ,则调用uncaughtException 方法

3.若没有父的ThreadGroup 没有全局默认的UncaughtExceptionHandler ,

    直接将异常的堆栈信息定向到System.err中

package com.zl.step7;

import java.util.concurrent.TimeUnit;

public class EmptyExceptionHandler {

    public static void main(String[] args) {
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();


        System.out.println(mainGroup.getName());
        System.out.println(mainGroup.getParent());
        System.out.println(mainGroup.getParent().getParent());

        final Thread thread = new Thread(()-> {

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(1/0);

        },"Test-Thread");

        thread.start();

    }


}

注入钩子线程

Java程序经常也会遇到进程挂掉的情况,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码。JAVA中的ShutdownHook提供了比较好的方案。

JDK提供了Java.Runtime.addShutdownHook(Thread hook)方法,可以注册一个JVM关闭的钩子,这个钩子可以在一下几种场景中被调用:

程序正常退出
使用System.exit()
终端使用Ctrl+C触发的中断
系统关闭
OutOfMemory宕机
使用Kill pid命令干掉进程(注:在使用kill -9 pid时,是不会被调用的)

代码样例

import java.util.concurrent.TimeUnit;

public class ThreadHook {
    public static void main(String[] args) {


        // 注入钩子线程
        Runtime.getRuntime().addShutdownHook(new Thread(){

            @Override
            public void run(){

                System.out.println("The hook thread 1 is running ....");

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                System.out.println("The hook thread 1 is exit  ....");

            }


        });



        // 钩子线程可以注册多个
        Runtime.getRuntime().addShutdownHook(new Thread(){

            @Override
            public void run(){

                System.out.println("The hook thread 2 is running ....");

                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


                System.out.println("The hook thread 2 is exit  ....");

            }


        });


        System.out.println(" The program will is stopping . ");

    }
}

输出结果:

The program will is stopping . 
The hook thread 1 is running ....
The hook thread 2 is running ....
The hook thread 1 is exit  ....
Disconnected from the target VM, address: '127.0.0.1:60787', transport: 'socket'
The hook thread 2 is exit  ....

Process finished with exit code 0

Hook线程实战

        在开发的过程中经常会遇到Hook线程,比如为了防止某个程序被重复启动,在进程启动的时候,会创建一个lock文件,进程收到中断信号的时候会删除这个lock文件,我们在mysql、zookeeper、kafka等系统中都会看到lock文件的存在。

 

模拟一个防止重复启动的程序:

package com.zl.step7;

import com.sun.tools.javac.code.Type;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class PreventDuplicated {

    private final  static String LOCK_PATH = "/A/" ;

    private final  static String LOCK_FILE = ".lock" ;

    private final static String   PERMISSIONS = "rw-------" ;

    public static void main(String[] args) throws IOException {

        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("The program received kill SIGNAL . ");
            getLockFile().toFile().delete();
        }));


       checkRunning();

       for (;;) {

           try {
               TimeUnit.SECONDS.sleep(1);

               System.out.println("program is running.");
               
           } catch (InterruptedException e) {
               e.printStackTrace();
           }

       }

    }

    private static void checkRunning() throws IOException {
        Path path = getLockFile();
        if(path.toFile().exists()){
            throw new RuntimeException("The program is already running .") ;
        }

        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(PERMISSIONS) ;

       Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));

    }


    private static Path getLockFile(){
        return Paths.get(LOCK_PATH,LOCK_FILE) ;
    }
}

在线程执行的时候在 /A路径下 会生成一个 .lock 文件

当杀掉进程之后,就会将该文件删除。

Hook线程应用场景,以及注意事项。

1.Hook线程只有在收到退出信号的时候才会被执行,如果在kill的时候,使用了参数 -9 , 

那么Hook线程不会得到执行,进程将立即退出,因此.lock文件将得不到清理。

2.Hook线程中也可以执行一些释放资源的操作,比如关闭文件句柄,socket连接,数据库connection等

3.尽量不要在Hook线程中执行一些耗时非常长的操作,因为其会导致程序迟迟不能退出。

4.如果强制杀死进程,那么进程间更不会收到任何中断信号。

本文来源于:

《Java高并发编程详解:多线程与架构设计》 --汪文君

猜你喜欢

转载自blog.csdn.net/zhanglong_4444/article/details/86073199