1 /**********************************************************************
2 * EJThreadFactory.java
3 * created on 10.08.2006 by netseeker
4 * $Id: EJThreadFactory.java,v 1.4 2006/11/10 00:35:14 netseeker Exp $
5 * $Log: EJThreadFactory.java,v $
6 * Revision 1.4 2006/11/10 00:35:14 netseeker
7 * switched to maven2
8 *
9 * Revision 1.3 2006/11/05 16:34:39 netseeker
10 * Code cleanup, added support of JDK 1.5 thread pools
11 *
12 * Revision 1.2 2006/08/10 18:57:06 netseeker
13 * *** empty log message ***
14 *
15 * Revision 1.1 2006/08/10 18:36:04 netseeker
16 * added abstraction of ThreadPools and an Executor based implementation for Java >= 1.5
17 *
18 *
19 * ====================================================================
20 *
21 * Copyright 2005-2006 netseeker aka Michael Manske
22 *
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26 *
27 * http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 * ====================================================================
35 *
36 * This file is part of the EJOE framework.
37 * For more information on the author, please see
38 * <http://www.manskes.de/>.
39 *
40 *********************************************************************/
41 package de.netseeker.ejoe.concurrent;
42
43 import java.util.concurrent.ThreadFactory;
44 import java.util.concurrent.atomic.AtomicInteger;
45
46 /***
47 * A ThreadFactory which allows usage of a custom ThreadGroup for the produced threads
48 *
49 * @author netseeker
50 * @since 0.3.9.1
51 */
52 public class EJThreadFactory implements ThreadFactory
53 {
54
55 private static final AtomicInteger _poolCount = new AtomicInteger( 1 );
56
57 private final AtomicInteger _threadCount = new AtomicInteger( 1 );
58
59 private final ThreadGroup _threadGroup;
60
61 final private String _prefix;
62
63 /***
64 * Creates a standard ThreadFactory which does behaves like the DefaultThreadFactory.
65 */
66 EJThreadFactory()
67 {
68 SecurityManager secMan = System.getSecurityManager();
69 _threadGroup = (secMan != null) ? secMan.getThreadGroup() : Thread.currentThread().getThreadGroup();
70 _prefix = "EJOE Threadpool-" + _poolCount.getAndIncrement() + "-thread-";
71 }
72
73 /***
74 * Creates a standard ThreadFactory which does use the given ThreadGroup for the produced threads
75 *
76 * @param threadGroup
77 */
78 EJThreadFactory(ThreadGroup threadGroup)
79 {
80 _threadGroup = threadGroup;
81 _prefix = "pool-" + _poolCount.getAndIncrement() + "-thread-";
82 }
83
84
85
86
87
88
89 public Thread newThread( Runnable r )
90 {
91 Thread t = new Thread( _threadGroup, r, _prefix + _threadCount.getAndIncrement(), 0 );
92 if ( t.isDaemon() ) t.setDaemon( false );
93 if ( t.getPriority() != Thread.NORM_PRIORITY ) t.setPriority( Thread.NORM_PRIORITY );
94 return t;
95 }
96
97 }