Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/emory/mathcs/util/concurrent/PlainThreadFactory.java


1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is the Emory Utilities.
15   *
16   * The Initial Developer of the Original Code is
17   * The Distributed Computing Laboratory, Emory University.
18   * Portions created by the Initial Developer are Copyright (C) 2002
19   * the Initial Developer. All Rights Reserved.
20   *
21   * Alternatively, the contents of this file may be used under the terms of
22   * either the GNU General Public License Version 2 or later (the "GPL"), or
23   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24   * in which case the provisions of the GPL or the LGPL are applicable instead
25   * of those above. If you wish to allow use of your version of this file only
26   * under the terms of either the GPL or the LGPL, and not to allow others to
27   * use your version of this file under the terms of the MPL, indicate your
28   * decision by deleting the provisions above and replace them with the notice
29   * and other provisions required by the GPL or the LGPL. If you do not delete
30   * the provisions above, a recipient may use your version of this file under
31   * the terms of any one of the MPL, the GPL or the LGPL.
32   *
33   * ***** END LICENSE BLOCK ***** */
34  
35  package edu.emory.mathcs.util.concurrent;
36  
37  import java.security.*;
38  
39  /**
40   * Thread factory implementation that attempts to ensure that created threads
41   * are equivalent regardless of threads that request creation. Precisely,
42   * created threads belong to the same thread group, and they inherit a
43   * "parent thread context" (access control context,
44   * {@link DelegatableThreadLocal}s, etc.) from the factory creator rather than
45   * from the invoker of {@link #newThread}.
46   * <p>
47   * This thread factory is used as a default for {@link PooledExecutor}, as it
48   * guarantees deterministic behavior and minimal security properties.
49   * Nevertheless, stronger semantics are often neccessary in
50   * security-sensitive applications. In particular,
51   * it may be required that worker tasks run within access control context and
52   * with delegatable locals of the thread that scheduled the task, which
53   * may generally be distinct from both the thread pool creator and the worker
54   * thread creator. If such security semantics is needed, use
55   * {@link SecurePooledExecutor}.
56   *
57   * @author Dawid Kurzyniec
58   * @version 1.0
59   */
60  
61  public class PlainThreadFactory implements ThreadFactory {
62      final AccessControlContext acc;
63      final ThreadGroup group;
64      final ThreadContext tc;
65      final String name;
66      int idx = 0;
67  
68      /**
69       * Creates a new tread factory that creates threads within a default
70       * thread group. The group of the current thread (invoking this
71       * constructor) is used as a default unless explicitly overridden
72       * by current security manager.
73       */
74      public PlainThreadFactory() {
75          this(null, null);
76      }
77  
78      /**
79       * Creates a new tread factory that uses default thread group and
80       * specified thread name prefix. The group of the current thread (invoking
81       * this constructor) is used as a default unless explicitly overridden
82       * by current security manager.
83       *
84       * @param name the thread name prefix
85       */
86      public PlainThreadFactory(String name) {
87          this(null, name);
88      }
89  
90      /**
91       * Creates a new tread factory that creates threads within the specified
92       * thread group.
93       *
94       * @param group the thread group for created threads
95       */
96      public PlainThreadFactory(ThreadGroup group) {
97          this(group, null);
98      }
99  
100     /**
101      * Creates a new tread factory that creates threads within the specified
102      * thread group, and uses specified thread name prefix.
103      *
104      * @param group the thread group for created threads
105      * @param name the thread name prefix
106      */
107     public PlainThreadFactory(ThreadGroup group, String name) {
108         if (group == null) {
109             // try to determine tg as in java.lang.Thread
110             SecurityManager security = System.getSecurityManager();
111 
112             if (security != null) {
113                 group = security.getThreadGroup();
114             }
115         }
116 
117         if (group == null) {
118             group = Thread.currentThread().getThreadGroup();
119         }
120 
121         this.group = group;
122         this.name = name;
123         this.acc = AccessController.getContext();
124         this.tc = ThreadContext.getContext();
125     }
126 
127     public Thread newThread(final Runnable command) {
128         return (Thread)AccessController.doPrivileged(new PrivilegedAction() {
129             public Object run() {
130                 Runnable commandWrapper = new DelegatedRunnable(command);
131                 if (name == null) {
132                     return new Thread(group, commandWrapper);
133                 }
134                 else {
135                     String tname = (name != null ? name + " " + getNextIdx() : null);
136                     return new Thread(group, commandWrapper, tname);
137                 }
138             }
139         }, acc);
140     }
141 
142     private synchronized int getNextIdx() {
143         return idx++;
144     }
145 }