文章目录
重拾并发编程实战(二)
第七章、Hook线程以及捕获线程执行异常
7.1 获取线程执行时异常
在Thread类里面,关于处理运行时异常的API共有4个,如下所示:
- public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh):为某个特定线程指定一个UncaughtExceptionHandler。
- public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh):设置全局的UncaughtExceptionHandler。
- public UncaughtExceptionHandler getUncaughtExceptionHandler():获取特定线程的UncaughtExceptionHandler。
- pubic static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler():获取全局的UncaughtExceptionHandler。
7.2 什么是UncaughtExceptionHandler?
线程在执行单元中不允许抛出checked异常的,而且线程运行在自己的上下文中,派生它的线程将无法直接获取它运行中出现的异常信息。对此,java为我们提供了一个UncaughtExceptionHandler接口,当线程在运行的过程中出现了异常,会回调UncaughtExceptionHandler接口,从而得知是哪个线程在运行时出错,以及出现了什么样的错误。
接口源码示例:
@FunctionalInterface
public interface UncaughtExceptionHandler{
/**
当线程运行过程出现异常,JVM会调用线程的一个方法:dispatchUncaughtException(),将对应的线程实例已经异常接口传递给回调接口。
而回调接口的这个方法所抛出的任何异常都会被JVM忽略。
*/
void uncaughtException(Thread t,Throwable e);//函数式接口
}
//线程的dispatchUncaughtException()方法
private void dispatchUncaughtException(Throwable e){
getUncaughtExceptionHandler().uncaughtException(this,e);
}
实例应用代码——UncaughtExceptinHandler
import java.util.concurrent.TimeUnit;
public class CaptureThreadException {
public static void main(String[] args) {
//设置回调接口UncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler((t,e)->{
System.out.println(t.getName()+" occur exception");
e.printStackTrace();
});
final Thread thread = new Thread(()->{
//run方法
try{
//睡眠两秒
TimeUnit.SECONDS.sleep(2);
}catch (InterruptedException e){
}
//这里会出现unchecked异常
System.out.println(1/0);
},"Test-Thread");
//启动线程
thread.start();
}//执行上面的程序,两秒后会抛出一个unchecked异常,回调接口可获取该异常信息而不是不给编译通过。
}
执行上面的程序,线程Test-Thread会运行两秒之后抛出unchecked异常,回调接口将获得该异常信息。
工作中这种涉及方式比较常见,尤其式那种异步执行的方法,比如Google的guava toolkit就提供了EvenBus,在EvenBus中,事件源和事件的subscriber两者借助于EvenBus实现了完全的解耦合,但是在subscriber执行任务时有可能会出现异常情况,EvenBus同样也借助于一个ExceptionHandler进行回调处理。
7.3 UncaughtExceptionHandler源码分析(执行过程)
public UncaughtExceptionHandler getUncaughtExceptionHandler(){
return uncaughtExceptionHandler !=null:uncaughtExceptionHandler:group;
}
- getUncaughtExceptionHandler()方法首先会判断当前的线程是否设置了handler,如果有,则调用自己的uncaughtException方法。否则就到所在的ThreadGroup中获取。
// 接口定义
public interface UncaughtExceptionHandler {
void uncaughtException(Thread t, Throwable e);
}
// 未捕获异常实例属性:未捕获异常
private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
// 未捕获异常静态属性:默认未捕获异常
private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
// 设置默认的未捕获异常
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(
new RuntimePermission("setDefaultUncaughtExceptionHandler")
);
}
defaultUncaughtExceptionHandler = eh;
}
// 获取默认未捕获异常
public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
return defaultUncaughtExceptionHandler;
}
// 获取未捕获异常
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
// 这里才是高潮,如果没有设置未捕获异常,那么就将group属性当作未捕获异常
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}
// 设置未捕获异常
public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
checkAccess();
uncaughtExceptionHandler = eh;
}
根据JDK文档的描述,当出现未捕获异常的时候,JVM调用的是Thread.getUncaughtExceptionHandler(),这个方法中,如果你没有调用Thread.setUncaughtExceptionHandler()设置未捕获异常处理器,那么将会返回Thread.group,将ThreadGroup当作未捕获异常处理器,而ThreadGroup实现了UncaughtExceptionHandler,所以转到ThreadGroup的uncaughtException(Thread, Throwable)方法。其源码为:
源码解析:
- 如果该ThreadGroup有父线程组,则直接调用父Group的uncaughtException方法。
- 如果设置了全局默认的UncaughtExceptionHandler,则调用其uncaughtException方法。(else里面的)
- 如果都没有则将异常的堆栈信息直接定向到System.err中。
实战代码——EmptyExceptionHandler
代码:
package chapter7_Hook;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class EmptyExceptionHandler {
public static void main(String[] args) {
//获取当前线程的线程组(main所在线程组)
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){
}
//这里会抛出uncheck exception
System.out.println(1/0);
},"Test-Thread");
//开始线程
thread.start();
}
}
运行结果:
我们来看一下它寻找uncaughtException的流程:
7.4 注入钩子线程(Hook)
JVM进程的退出时由于JVM进程中没有活跃的非守护线程,或者收到了系统中断 信号,向JVM程序注入了一个Hook线程,在JVM进程退出的时候,Hook线程启动执行,通过Runtime可以为JVM注入多个Hook线程。
测试代码:
package chapter7_Hook;
import java.util.concurrent.TimeUnit;
/**
* JVM进程的退出时由于JVM进程中没有活跃的非守护线程,或者收到了系统中断 信号,向JVM程序注入了一个Hook线程,
* 在JVM进程退出的时候,Hook线程启动执行,通过Runtime可以为JVM注入多个Hook线程。
*/
public class HookThread {
public static void main(String[] args) {
//为应用注入钩子进程
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
try{
System.out.println("The hook Thread 1 is running .");
//睡眠1秒
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("The hook thread 1 will exit.");
}
});
//钩子线程可以注册多个
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
try{
System.out.println("The hook Thread 2 is running .");
//睡眠1秒
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("The hook thread 2 will exit.");
}
});
//主线程用于验证结束时钩子线程启动
System.out.println("The main program will exit!");
}
}
运行结果:
7.5 Hook线程实战
模拟一个防止重新启动的程序,代码如下:
package chapter7_Hook;
//利用Hook线程,可以实现某个程序的防止重复启动功能。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
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 = "E:\\java\\";
private final static String LOCK_FILE = "1.lock";
private final static String PERMISSIONS = "rw-------";
public static void main(String[] args) throws IOException {
//1、注入Hook线程,在程序退出的时候删除lock文件
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("The program received kill SIGNAL.");
getLockFile().toFile().delete();
}
});
//2、检测是否存在.lock文件
checkRunning();
//3、简单模拟当前程序在运行
for (; ; ) {
try {
//睡眠1毫秒
TimeUnit.MILLISECONDS.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!");
///返回与给定String表示相对应的权限集
//Set<PosixFilePermission> perms = PosixFilePermissions.fromString(PERMISSIONS);
//按照指定的权限创建文件
//Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
File f = new File(LOCK_PATH+LOCK_FILE);
while (!f.exists()){
f.createNewFile();
f.setExecutable(true,false);
f.setReadable(true);
f.setWritable(true);
}
//得到警告'posix:permissions' not supported as initial attribute
// 原因:PosixFilePermission只能将其用于与POSIX兼容的操作系统
//Windows不幸地不支持POSIX文件系统,因此这就是您的代码不起作用的原因。为了在Windows中创建目录,您应该使用:
}
private static Path getLockFile() {
return Paths.get(LOCK_PATH, LOCK_FILE);
}
}
我门的运行环境已经打开一个程序,使用CMD试着重新打开一个
完成,发现无法重复运行,原因是已经有一个.lock文件存在了。
7.6 小结
-
当一个线程抛出异常,如果没有显式处理(即try catch),JVM会将该异常事件报告给该线程对象的Java.lang.Thread.UncaughtExceptionHandler,如果没有设置UncaughtExceptionHandler,那么默认将会把异常栈信息输出到System.err。
所以,无论是线程池也好,自己写的线程也好,如果你希望他在挂了的时候能给到你通知并处理一下,除了显示处理,还可以提供未捕获异常处理器。 -
Hook的应用:
防止程序重复执行,具体实现可以在程序启动时,校验是否已经生成 lock 文件,如果已经生成,则退出程序,如果未生成,则生成 lock 文件,程序正常执行,最后再注入 Hook 线程,这样在 JVM 退出的时候,线程中再将 lock 文件删除掉;
这种防止程序重复执行的策略,也被应用于 Mysql 服务器,zookeeper, kafka 等系统中。
Hook 线程中也可以执行一些资源释放的操作,比如关闭数据库连接,Socket 连接等。
所以,无论是线程池也好,自己写的线程也好,如果你希望他在挂了的时候能给到你通知并处理一下,除了显示处理,还可以提供未捕获异常处理器。