Path: blob/master/src/java.rmi/share/classes/sun/rmi/runtime/NewThreadAction.java
41153 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.rmi.runtime;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import sun.security.util.SecurityConstants;3031/**32* A PrivilegedAction for creating a new thread conveniently with an33* AccessController.doPrivileged construct.34*35* All constructors allow the choice of the Runnable for the new36* thread to execute, the name of the new thread (which will be37* prefixed with "RMI "), and whether or not it will be a daemon38* thread.39*40* The new thread may be created in the system thread group (the root41* of the thread group tree) or an internally created non-system42* thread group, as specified at construction of this class.43*44* The new thread will have the system class loader as its initial45* context class loader (that is, its context class loader will NOT be46* inherited from the current thread).47*48* @author Peter Jones49**/50public final class NewThreadAction implements PrivilegedAction<Thread> {5152/** cached reference to the system (root) thread group */53@SuppressWarnings("removal")54static final ThreadGroup systemThreadGroup =55AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {56public ThreadGroup run() {57ThreadGroup group = Thread.currentThread().getThreadGroup();58ThreadGroup parent;59while ((parent = group.getParent()) != null) {60group = parent;61}62return group;63}64});6566/**67* special child of the system thread group for running tasks that68* may execute user code, so that the security policy for threads in69* the system thread group will not apply70*/71@SuppressWarnings("removal")72static final ThreadGroup userThreadGroup =73AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {74public ThreadGroup run() {75return new ThreadGroup(systemThreadGroup, "RMI Runtime");76}77});7879private final ThreadGroup group;80private final Runnable runnable;81private final String name;82private final boolean daemon;8384NewThreadAction(ThreadGroup group, Runnable runnable,85String name, boolean daemon)86{87this.group = group;88this.runnable = runnable;89this.name = name;90this.daemon = daemon;91}9293/**94* Creates an action that will create a new thread in the95* system thread group.96*97* @param runnable the Runnable for the new thread to execute98*99* @param name the name of the new thread100*101* @param daemon if true, new thread will be a daemon thread;102* if false, new thread will not be a daemon thread103*/104public NewThreadAction(Runnable runnable, String name, boolean daemon) {105this(systemThreadGroup, runnable, name, daemon);106}107108/**109* Creates an action that will create a new thread.110*111* @param runnable the Runnable for the new thread to execute112*113* @param name the name of the new thread114*115* @param daemon if true, new thread will be a daemon thread;116* if false, new thread will not be a daemon thread117*118* @param user if true, thread will be created in a non-system119* thread group; if false, thread will be created in the system120* thread group121*/122public NewThreadAction(Runnable runnable, String name, boolean daemon,123boolean user)124{125this(user ? userThreadGroup : systemThreadGroup,126runnable, name, daemon);127}128129public Thread run() {130@SuppressWarnings("removal")131SecurityManager sm = System.getSecurityManager();132if (sm != null) {133sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);134}135Thread t = new Thread(group, runnable, "RMI " + name);136t.setContextClassLoader(ClassLoader.getSystemClassLoader());137t.setDaemon(daemon);138return t;139}140}141142143