這個方法的含義說明:
這個方法的意思就是在jvm中增加一個關閉的鉤子,當jvm關閉的時候,會執行系統中已經設置的所有通過方法addShutdownHook添加的鉤子,當系統執行完這些鉤子后,jvm才會關閉。所以這些鉤子可以在jvm關閉的時候進行內存清理、對象銷毀等操作。
測試類如下:
- public class RunTimeTest {
-
-
-
- public static void main(String[] args) {
- Thread thread1 = new Thread() {
- public void run() {
- System.out.println("thread1...");
- }
- };
- Thread thread2 = new Thread() {
- public void run() {
- System.out.println("thread2...");
- }
- };
- Thread shutdownThread = new Thread() {
- public void run() {
- System.out.println("shutdownThread...");
- }
- };
- Runtime.getRuntime().addShutdownHook(shutdownThread);
- thread1.start();
- thread2.start();
- }
- }
打印結果為:
thread2...
thread1...
shutdownThread...
或者:
thread2...
thread1...
shutdownThread...
結論:
無論是先打印thread1還是thread2,shutdownThread 線程都是最后執行的(因為這個線程是在jvm執行關閉前才會執行)。
posted on 2013-05-23 16:34
Terry Zou 閱讀(233)
評論(0) 編輯 收藏 所屬分類:
Android