changeset 9217:a13bafb05072

8172204: Better Thread Pool execution Reviewed-by: alanb, skoivu, rriggs
author igerasim
date Fri, 14 Jul 2017 04:40:15 +0100
parents f2a67bbbcb99
children f014749131f6
files src/share/classes/java/util/concurrent/ThreadPoolExecutor.java
diffstat 1 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java	Wed Dec 21 10:15:49 2016 -0500
+++ b/src/share/classes/java/util/concurrent/ThreadPoolExecutor.java	Fri Jul 14 04:40:15 2017 +0100
@@ -34,6 +34,10 @@
  */
 
 package java.util.concurrent;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.concurrent.locks.AbstractQueuedSynchronizer;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.ReentrantLock;
@@ -567,6 +571,9 @@
     private static final RuntimePermission shutdownPerm =
         new RuntimePermission("modifyThread");
 
+    /* The context to be used when executing the finalizer, or null. */
+    private final AccessControlContext acc;
+
     /**
      * Class Worker mainly maintains interrupt control state for
      * threads running tasks, along with other minor bookkeeping.
@@ -1310,6 +1317,9 @@
             throw new IllegalArgumentException();
         if (workQueue == null || threadFactory == null || handler == null)
             throw new NullPointerException();
+        this.acc = System.getSecurityManager() == null ?
+                null :
+                AccessController.getContext();
         this.corePoolSize = corePoolSize;
         this.maximumPoolSize = maximumPoolSize;
         this.workQueue = workQueue;
@@ -1475,9 +1485,24 @@
     /**
      * Invokes {@code shutdown} when this executor is no longer
      * referenced and it has no threads.
+     *
+     * <p>This method is invoked with privileges that are restricted by
+     * the security context of the caller that invokes the constructor.
      */
     protected void finalize() {
-        shutdown();
+        SecurityManager sm = System.getSecurityManager();
+        if (sm == null || acc == null) {
+            shutdown();
+        } else {
+            PrivilegedAction<Void> pa = new PrivilegedAction<Void>() {
+                @Override
+                public Void run() {
+                    shutdown();
+                    return null;
+                }
+            };
+            AccessController.doPrivileged(pa, acc);
+        }
     }
 
     /**
@@ -2099,4 +2124,3 @@
         }
     }
 }
-