src/java.rmi/share/classes/sun/rmi/runtime/NewThreadAction.java
changeset 47216 71c04702a3d5
parent 25859 3317bb8137f4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.rmi.runtime;
       
    27 
       
    28 import java.security.AccessController;
       
    29 import java.security.PrivilegedAction;
       
    30 import sun.security.util.SecurityConstants;
       
    31 
       
    32 /**
       
    33  * A PrivilegedAction for creating a new thread conveniently with an
       
    34  * AccessController.doPrivileged construct.
       
    35  *
       
    36  * All constructors allow the choice of the Runnable for the new
       
    37  * thread to execute, the name of the new thread (which will be
       
    38  * prefixed with "RMI "), and whether or not it will be a daemon
       
    39  * thread.
       
    40  *
       
    41  * The new thread may be created in the system thread group (the root
       
    42  * of the thread group tree) or an internally created non-system
       
    43  * thread group, as specified at construction of this class.
       
    44  *
       
    45  * The new thread will have the system class loader as its initial
       
    46  * context class loader (that is, its context class loader will NOT be
       
    47  * inherited from the current thread).
       
    48  *
       
    49  * @author      Peter Jones
       
    50  **/
       
    51 public final class NewThreadAction implements PrivilegedAction<Thread> {
       
    52 
       
    53     /** cached reference to the system (root) thread group */
       
    54     static final ThreadGroup systemThreadGroup =
       
    55         AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {
       
    56             public ThreadGroup run() {
       
    57                 ThreadGroup group = Thread.currentThread().getThreadGroup();
       
    58                 ThreadGroup parent;
       
    59                 while ((parent = group.getParent()) != null) {
       
    60                     group = parent;
       
    61                 }
       
    62                 return group;
       
    63             }
       
    64         });
       
    65 
       
    66     /**
       
    67      * special child of the system thread group for running tasks that
       
    68      * may execute user code, so that the security policy for threads in
       
    69      * the system thread group will not apply
       
    70      */
       
    71     static final ThreadGroup userThreadGroup =
       
    72         AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {
       
    73             public ThreadGroup run() {
       
    74                 return new ThreadGroup(systemThreadGroup, "RMI Runtime");
       
    75             }
       
    76         });
       
    77 
       
    78     private final ThreadGroup group;
       
    79     private final Runnable runnable;
       
    80     private final String name;
       
    81     private final boolean daemon;
       
    82 
       
    83     NewThreadAction(ThreadGroup group, Runnable runnable,
       
    84                     String name, boolean daemon)
       
    85     {
       
    86         this.group = group;
       
    87         this.runnable = runnable;
       
    88         this.name = name;
       
    89         this.daemon = daemon;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Creates an action that will create a new thread in the
       
    94      * system thread group.
       
    95      *
       
    96      * @param   runnable the Runnable for the new thread to execute
       
    97      *
       
    98      * @param   name the name of the new thread
       
    99      *
       
   100      * @param   daemon if true, new thread will be a daemon thread;
       
   101      * if false, new thread will not be a daemon thread
       
   102      */
       
   103     public NewThreadAction(Runnable runnable, String name, boolean daemon) {
       
   104         this(systemThreadGroup, runnable, name, daemon);
       
   105     }
       
   106 
       
   107     /**
       
   108      * Creates an action that will create a new thread.
       
   109      *
       
   110      * @param   runnable the Runnable for the new thread to execute
       
   111      *
       
   112      * @param   name the name of the new thread
       
   113      *
       
   114      * @param   daemon if true, new thread will be a daemon thread;
       
   115      * if false, new thread will not be a daemon thread
       
   116      *
       
   117      * @param   user if true, thread will be created in a non-system
       
   118      * thread group; if false, thread will be created in the system
       
   119      * thread group
       
   120      */
       
   121     public NewThreadAction(Runnable runnable, String name, boolean daemon,
       
   122                            boolean user)
       
   123     {
       
   124         this(user ? userThreadGroup : systemThreadGroup,
       
   125              runnable, name, daemon);
       
   126     }
       
   127 
       
   128     public Thread run() {
       
   129         SecurityManager sm = System.getSecurityManager();
       
   130         if (sm != null) {
       
   131             sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
       
   132         }
       
   133         Thread t = new Thread(group, runnable, "RMI " + name);
       
   134         t.setContextClassLoader(ClassLoader.getSystemClassLoader());
       
   135         t.setDaemon(daemon);
       
   136         return t;
       
   137     }
       
   138 }