jdk/src/java.desktop/share/classes/com/sun/imageio/stream/StreamCloser.java
changeset 29922 7b9c1e1532cf
parent 25859 3317bb8137f4
child 31653 d88ff422c7fb
--- a/jdk/src/java.desktop/share/classes/com/sun/imageio/stream/StreamCloser.java	Fri Aug 01 13:31:03 2014 +0200
+++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/stream/StreamCloser.java	Fri Apr 03 17:17:36 2015 +0300
@@ -25,7 +25,12 @@
 
 package com.sun.imageio.stream;
 
+import sun.awt.util.ThreadGroupUtils;
+import sun.misc.InnocuousThread;
+
 import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Set;
 import java.util.WeakHashMap;
 import javax.imageio.stream.ImageInputStream;
@@ -81,27 +86,25 @@
                     }
                 };
 
-                java.security.AccessController.doPrivileged(
-                    new java.security.PrivilegedAction<Object>() {
-                        public Object run() {
-                            /* The thread must be a member of a thread group
-                             * which will not get GCed before VM exit.
-                             * Make its parent the top-level thread group.
-                             */
-                            ThreadGroup tg =
-                                Thread.currentThread().getThreadGroup();
-                            for (ThreadGroup tgn = tg;
-                                 tgn != null;
-                                 tg = tgn, tgn = tg.getParent());
-                            streamCloser = new Thread(tg, streamCloserRunnable);
-                            /* Set context class loader to null in order to avoid
-                             * keeping a strong reference to an application classloader.
-                             */
-                            streamCloser.setContextClassLoader(null);
-                            Runtime.getRuntime().addShutdownHook(streamCloser);
-                            return null;
-                        }
-                    });
+                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
+                    if (System.getSecurityManager() == null) {
+                        /* The thread must be a member of a thread group
+                         * which will not get GCed before VM exit.
+                         * Make its parent the top-level thread group.
+                         */
+                        ThreadGroup tg = ThreadGroupUtils.getRootThreadGroup();
+                        streamCloser = new Thread(tg, streamCloserRunnable);
+                    } else {
+                        /* InnocuousThread is a member of a correct TG by default */
+                        streamCloser = new InnocuousThread(streamCloserRunnable);
+                    }
+                    /* Set context class loader to null in order to avoid
+                     * keeping a strong reference to an application classloader.
+                     */
+                    streamCloser.setContextClassLoader(null);
+                    Runtime.getRuntime().addShutdownHook(streamCloser);
+                    return null;
+                });
             }
         }
     }