package com.aircoder.test;
import java.util.Date;
public class Test {
public static void sysLog(String str){
System.out.println(new Date()+"\t "+str);
}
public static void main(String[] args) {
System.out.println("\t\fHello world ");
sysLog("\t start service .....");
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
System.out.println("是结束了");
}
});
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
System.out.println("是结束了2");
}
});
//System.exit(1); kill 会执行,挨个执行钩子函数, kill -3 会打印线程堆栈,线程不会结束,kill -9 不会执行
while(true){
}
}
}
import java.util.Date;
public class Test {
public static void sysLog(String str){
System.out.println(new Date()+"\t "+str);
}
public static void main(String[] args) {
System.out.println("\t\fHello world ");
sysLog("\t start service .....");
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
System.out.println("是结束了");
}
});
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
System.out.println("是结束了2");
}
});
//System.exit(1); kill 会执行,挨个执行钩子函数, kill -3 会打印线程堆栈,线程不会结束,kill -9 不会执行
while(true){
}
}
}
本文介绍了一个简单的Java程序,演示了如何使用Java的Runtime类添加运行时关闭钩子。通过两个示例展示了在应用程序正常退出或被强制终止前执行特定清理任务的方法。
1344

被折叠的 条评论
为什么被折叠?



