1、获取线程运行时异常
1.1、在Thread类中,关于处理运行时异常的API总共有四个,如下所示:
public static void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) :为某个特定线程指定UncaughtExceptionHandler
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) :设置全局的UncaughtExceptionHandler
public static void getUncaughtExceptionHandler(UncaughtExceptionHandler eh) :获取特定线程的UncaughtExceptionHandler
public static void getDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh):获取全局的UncaughtExceptionHandler
1.2、UncaughtExceptionHandler的介绍
线程在执行单元中时不允许抛出checked异常的,而且线程运行在自己的上下文中,派生他的线程将无法直接获得它运行中出现的异常信息。对此,
Java为我们提供了一个UncaughtExceptionHandler接口,当线程在运行过程中出现异常时,会回调UncaughtExceptionHandler接口,从而得知时那个线程在
运行时出错,以及出现了什么样的错误,源码如下:
@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是一个FunctionInterface,只有一个抽象方法,该回调接口会被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会调用Thread对象的dispatchUncaughtException方法,该方法会将对应的线程实例以及异常信息传递给回调接口。
1.3 UncaughtExceptionHandler实例
public class CaptureThreadException {
public static void main(String[] args) {
//设置全局异常回调接口
Thread.setDefaultUncaughtExceptionHandler((t,e)->{
System.out.println(t.getName()+" occur exception");
e.printStackTrace();
});
final Thread thread=new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
}
//here will throw unchecked exception
System.out.println(1/0);
},"Test-Thread");
thread.start();
}
}
执行上面的程序,线程Test-Thread在运行两秒之后会抛出一个unchecked异常,我们设置的回调接口将获得该异常信息,程序的执行结果如下:
1.4 UncaughtExceptionHandler源码分析
在没有向线程注入UncaughtExceptionHandler回调接口的情况下,线程若出现了异常又将如何处理呢?下面我们将通过对Thread的源码进行分析来追踪一下,示例代码如下:
/**
* 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,如果有则执行线程自己的uncaughtException方法,否则就到所在的ThreadGroup中获取,ThreadGroup同样也实现了UncaughtExceptionHandler接口,下面再来看看ThreadGroup的uncaughtException方法:
/**
* 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)如果设置了全局默认的UncaughtExcpetionHandler,则调用uncaughtException方法
(3)若既没有父ThreadGroup,也没有设置全局默认的UncaughtExceptionHandler,则会直接将异常的堆栈信息定向到Systerm.err中
下面是没有设置默认的Handler,也没有对thread指定Handler,因此当thread出现异常时,会向上寻找Group的uncaughtException方法
public class EmptyExceptionHandler {
public static void main(String[] args) {
//get current thread's thread group
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) {
}
//here will throw unchecked exception
System.out.println(1/0);
},"Test-Thread");
thread.start();
}
}
执行结果:
2、注入钩子线程
2.1 Hook线程介绍
JVM进程的退出是由于JVM进程中没有活跃的非守护线程,或者收到了系统中断信号,向JVM程序注入一个Hook线程,在JVM进程退出的时候,Hook线程会启动执行,通过Runtime可以为JVM注入多个Hook线程,下面就通过一个简单的例子来看一下如何向Java程序注入Hook线程
public class ThreadHook {
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.");
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.");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("the hook thread 2 will exit.");
}
});
System.out.println("The program will be stopping.");
}
}
在上面代码中,给Java程序注入了两个Hook线程,在main线程中结束,也就是JVM中没有了活动的非守护线程,JVM进程即将退出时,两个Hook线程会被启动并且运行,输出结果如下:
2.2 Hook线程实战
在我们的开发中经常会遇到Hook线程,比如为了防止某个程序被重复启动,在进程启动时会创建一个lock文件,进程收到中断信号的时候会删除这个lock文件,我们再mysql服务器、zookeeper、kafka等系统中都能看到lock文件的存在,下面,将利用hook线程的特点,模拟一个防止重复启动的程序:
由于部分Java的api在linux和windows上面不同,所以准备了两个版本测试:
https://www.it1352.com/970454.html
windows版:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class PreventDuplicatedWindows {
private final static String LOCK_FILE="/home/admin/prevent-duplicated-windows.lock";
public static void main(String[] args) throws IOException {
//2、检查是否存在lock文件
checkRunning();
//1、注入Hook线程,在程序退出时删除lock文件
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("The program received kill SIGNAL.");
getLockFile().delete();
}));
//3、简单模拟当前程序运行
for (int i=1;i<10;i++){
try {
TimeUnit.SECONDS.sleep(1);
System.out.println("program is running."+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void checkRunning() throws IOException {
File file = getLockFile();
if(file.exists()){
throw new RuntimeException("The program already running.");
}else{
file.setReadable(true);//读
file.setWritable(true);//写
file.setExecutable(false);//执行
file.createNewFile();
}
}
private static File getLockFile() {
File file = new File(LOCK_FILE);
return file;
}
}
linux版:
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 PreventDuplicatedLinux {
private final static String LOCK_PATH="prevent-duplicated-linux";
private final static String LOCK_FILE=".lock";
private final static String PERMISSIONS="rw-------";
public static void main(String[] args) throws IOException {
//2、检查是否存在lock文件
checkRunning();
//1、注入Hook线程,在程序退出时删除lock文件
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("The program received kill SIGNAL.");
getLockFile().toFile().delete();
}));
//3、简单模拟当前程序运行
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 already running.");
}
Set<PosixFilePermission> permissionSet = PosixFilePermissions.fromString(PERMISSIONS);
Files.createFile(path,PosixFilePermissions.asFileAttribute(permissionSet));
}
private static Path getLockFile() {
return Paths.get(LOCK_PATH,LOCK_FILE);
}
}
测试结果,在程序运行期间只能同时运行一个,重复运行会抛出异常:The program already running.
2.3 Hook线程应用场景以及注意事项
(1)Hook线程只有在收到退出信号的时候会被执行,如果在kill的时候使用了参数-9,那么Hook线程不会得到执行,进程将会立即退出,因此lock文件将得不到清理。
(2)Hook线程中也可以执行一些资源释放的工作,比如关闭文件句柄、socket链接、数据库connection等
(3)尽量不要再Hook线程中执行一些耗时非常长的工作,因为其会导致程序迟迟不能退出。