ShutdownHook 是这样一个概念:向虚拟机注册一个线程,当程序退出(Ctrl+C)时虚拟机会启动这个线程,我们可以在这个线程的run()中做一些清除的工作,如:释放数据库连接,关闭文件等.
注册:
Runtime.getRuntime().addShutdownHook(Thread t);
注销:
Runtime.getRuntime().removeShutdownHook(Thread t);
请注意, 该 hook 线程必须是已经初始化但是没有运行的线程, 这个线程将在虚拟机响应用户的中断之前运行, 例如按下 ^C, 或者系统范围的事件, 例如注销或者关闭系统时.
[例子]
/**
* 在这个线程中实现程序退出前的清理工作
*
* @author Administrator
*
*/
class TestThread extends Thread {
boolean isTerminal = false;
public void run() {
while (!isTerminal) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run sub thread");
}
}
/**
* 清理工作
*/
public void onTerminal() {
isTerminal = true;
System.out.println("stop sun sub thread");
}
}
* 在这个线程中实现程序退出前的清理工作
*
* @author Administrator
*
*/
class TestThread extends Thread {
boolean isTerminal = false;
public void run() {
while (!isTerminal) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run sub thread");
}
}
/**
* 清理工作
*/
public void onTerminal() {
isTerminal = true;
System.out.println("stop sun sub thread");
}
}
/**
* ShutdownDownHook测试类
*
* @author Administrator
*
*/
public class TestShutdownHook extends Thread {
TestThread testThread;
public void addThread(TestThread t) {
testThread = t;
}
/**
* 实现程序退出前的清理工作
*/
public void run() {
System.out.println("This is ShutdownHook");
testThread.onTerminal();
}
public static void main(String[] args) {
TestShutdownHook m = new TestShutdownHook();
TestThread t = new TestThread();
t.start();
m.addThread(t);
// 注册退出处理线程
Runtime.getRuntime().addShutdownHook(m);
}
}
* ShutdownDownHook测试类
*
* @author Administrator
*
*/
public class TestShutdownHook extends Thread {
TestThread testThread;
public void addThread(TestThread t) {
testThread = t;
}
/**
* 实现程序退出前的清理工作
*/
public void run() {
System.out.println("This is ShutdownHook");
testThread.onTerminal();
}
public static void main(String[] args) {
TestShutdownHook m = new TestShutdownHook();
TestThread t = new TestThread();
t.start();
m.addThread(t);
// 注册退出处理线程
Runtime.getRuntime().addShutdownHook(m);
}
}
运行结果:
run sub thread
run sub thread
run sub thread
run sub thread
This is ShutdownHook
stop sun sub thread
run sub thread
run sub thread
run sub thread
This is ShutdownHook
stop sun sub thread
可以看到:当程序退出时启动了TestThread线程,执行了定义的释放工作。