提交 d034675c 编写于 作者: I igerasim

8172204: Better Thread Pool execution

Reviewed-by: alanb, skoivu, rriggs
上级 50874d19
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
*/ */
package java.util.concurrent; 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.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
...@@ -569,6 +573,9 @@ public class ThreadPoolExecutor extends AbstractExecutorService { ...@@ -569,6 +573,9 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
private static final RuntimePermission shutdownPerm = private static final RuntimePermission shutdownPerm =
new RuntimePermission("modifyThread"); 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 * Class Worker mainly maintains interrupt control state for
* threads running tasks, along with other minor bookkeeping. * threads running tasks, along with other minor bookkeeping.
...@@ -1307,6 +1314,9 @@ public class ThreadPoolExecutor extends AbstractExecutorService { ...@@ -1307,6 +1314,9 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null) if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException(); throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize; this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize; this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue; this.workQueue = workQueue;
...@@ -1472,9 +1482,18 @@ public class ThreadPoolExecutor extends AbstractExecutorService { ...@@ -1472,9 +1482,18 @@ public class ThreadPoolExecutor extends AbstractExecutorService {
/** /**
* Invokes {@code shutdown} when this executor is no longer * Invokes {@code shutdown} when this executor is no longer
* referenced and it has no threads. * 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() { protected void finalize() {
shutdown(); SecurityManager sm = System.getSecurityManager();
if (sm == null || acc == null) {
shutdown();
} else {
PrivilegedAction<Void> pa = () -> { shutdown(); return null; };
AccessController.doPrivileged(pa, acc);
}
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册