# HG changeset patch # User igerasim # Date 1500003615 -3600 # Node ID a13bafb05072a75e02256b2d976c7246601b9528 # Parent f2a67bbbcb9987d8d14fbcd002a61f5e781e80a1 8172204: Better Thread Pool execution Reviewed-by: alanb, skoivu, rriggs diff -r f2a67bbbcb99 -r a13bafb05072 src/share/classes/java/util/concurrent/ThreadPoolExecutor.java --- 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. + * + *

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 pa = new PrivilegedAction() { + @Override + public Void run() { + shutdown(); + return null; + } + }; + AccessController.doPrivileged(pa, acc); + } } /** @@ -2099,4 +2124,3 @@ } } } -