Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/runtime/NewThreadAction.java
38830 views
/*1* Copyright (c) 2000, 2005, 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 */53static final ThreadGroup systemThreadGroup =54AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {55public ThreadGroup run() {56ThreadGroup group = Thread.currentThread().getThreadGroup();57ThreadGroup parent;58while ((parent = group.getParent()) != null) {59group = parent;60}61return group;62}63});6465/**66* special child of the system thread group for running tasks that67* may execute user code, so that the security policy for threads in68* the system thread group will not apply69*/70static final ThreadGroup userThreadGroup =71AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {72public ThreadGroup run() {73return new ThreadGroup(systemThreadGroup, "RMI Runtime");74}75});7677private final ThreadGroup group;78private final Runnable runnable;79private final String name;80private final boolean daemon;8182NewThreadAction(ThreadGroup group, Runnable runnable,83String name, boolean daemon)84{85this.group = group;86this.runnable = runnable;87this.name = name;88this.daemon = daemon;89}9091/**92* Creates an action that will create a new thread in the93* system thread group.94*95* @param runnable the Runnable for the new thread to execute96*97* @param name the name of the new thread98*99* @param daemon if true, new thread will be a daemon thread;100* if false, new thread will not be a daemon thread101*/102public NewThreadAction(Runnable runnable, String name, boolean daemon) {103this(systemThreadGroup, runnable, name, daemon);104}105106/**107* Creates an action that will create a new thread.108*109* @param runnable the Runnable for the new thread to execute110*111* @param name the name of the new thread112*113* @param daemon if true, new thread will be a daemon thread;114* if false, new thread will not be a daemon thread115*116* @param user if true, thread will be created in a non-system117* thread group; if false, thread will be created in the system118* thread group119*/120public NewThreadAction(Runnable runnable, String name, boolean daemon,121boolean user)122{123this(user ? userThreadGroup : systemThreadGroup,124runnable, name, daemon);125}126127public Thread run() {128SecurityManager sm = System.getSecurityManager();129if (sm != null) {130sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);131}132Thread t = new Thread(group, runnable, "RMI " + name);133t.setContextClassLoader(ClassLoader.getSystemClassLoader());134t.setDaemon(daemon);135return t;136}137}138139140