Java资源回收
资源回收的时机
- 程序kill -9与kill -15的区别,以及回调函数的作用 - ungoneless的博客 - CSDN博客
1
2
3
4
5
6
7
8
9
10
11
12public class ShutdownHookTest {
private static final void shutdownCallback() {
System.out.println("Shutdown callback is invoked.");
// 在这回收资源
}
public static void main(String[] args) throws InterruptedException {
Runtime.getRuntime().addShutdownHook(new Thread(ShutdownHookTest::shutdownCallback));
Thread.sleep(20000);
}
}
1 | java ShutdownHookTest |