jdk/src/share/classes/java/io/DeleteOnExitHook.java
changeset 2703 acd4d6a53e3e
parent 2277 445a331b4a8b
child 5506 202f599c92aa
--- a/jdk/src/share/classes/java/io/DeleteOnExitHook.java	Thu Apr 23 19:44:43 2009 +0100
+++ b/jdk/src/share/classes/java/io/DeleteOnExitHook.java	Mon Apr 27 12:08:41 2009 -0700
@@ -34,23 +34,31 @@
  */
 
 class DeleteOnExitHook {
+    private static LinkedHashSet<String> files = new LinkedHashSet<String>();
     static {
-         sun.misc.SharedSecrets.getJavaLangAccess()
-             .registerShutdownHook(2 /* Shutdown hook invocation order */,
-                 new Runnable() {
-                     public void run() {
-                        runHooks();
-                     }
-                 });
+        // DeleteOnExitHook must be the last shutdown hook to be invoked.
+        // Application shutdown hooks may add the first file to the
+        // delete on exit list and cause the DeleteOnExitHook to be
+        // registered during shutdown in progress. So set the
+        // registerShutdownInProgress parameter to true.
+        sun.misc.SharedSecrets.getJavaLangAccess()
+            .registerShutdownHook(2 /* Shutdown hook invocation order */,
+                true /* register even if shutdown in progress */,
+                new Runnable() {
+                    public void run() {
+                       runHooks();
+                    }
+                }
+        );
     }
 
-    private static LinkedHashSet<String> files = new LinkedHashSet<String>();
-
     private DeleteOnExitHook() {}
 
     static synchronized void add(String file) {
-        if(files == null)
+        if(files == null) {
+            // DeleteOnExitHook is running. Too late to add a file
             throw new IllegalStateException("Shutdown in progress");
+        }
 
         files.add(file);
     }