1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.concurrent.ThreadFactory;
18  
19  /**
20   * <a href="NamedThreadFactory.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Michael C. Han
23   */
24  public class NamedThreadFactory implements ThreadFactory {
25  
26      public NamedThreadFactory(
27          String name, int priority, ClassLoader contextClassLoader) {
28  
29          SecurityManager securityManager = System.getSecurityManager();
30  
31          if (securityManager != null) {
32              _group = securityManager.getThreadGroup();
33          }
34          else {
35              Thread currentThread = Thread.currentThread();
36  
37              _group = currentThread.getThreadGroup();
38          }
39  
40          _name = name;
41          _priority = priority;
42          _contextClassLoader = contextClassLoader;
43      }
44  
45      public Thread newThread(Runnable runnable) {
46          Thread thread = new Thread(_group, runnable, _name);
47  
48          thread.setDaemon(true);
49          thread.setPriority(_priority);
50  
51          if (_contextClassLoader != null) {
52              thread.setContextClassLoader(_contextClassLoader);
53          }
54  
55          return thread;
56      }
57  
58      private ClassLoader _contextClassLoader;
59      private ThreadGroup _group;
60      private String _name;
61      private int _priority;
62  
63  }