1 /*
2  * Copyright 2013 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at:
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16
17 package io.netty.util.concurrent;
18
19 import io.netty.util.internal.ObjectUtil;
20 import io.netty.util.internal.StringUtil;
21
22 import java.util.Locale;
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 /**
27  * A {@link ThreadFactory} implementation with a simple naming rule.
28  */

29 public class DefaultThreadFactory implements ThreadFactory {
30
31     private static final AtomicInteger poolId = new AtomicInteger();
32
33     private final AtomicInteger nextId = new AtomicInteger();
34     private final String prefix;
35     private final boolean daemon;
36     private final int priority;
37     protected final ThreadGroup threadGroup;
38
39     public DefaultThreadFactory(Class<?> poolType) {
40         this(poolType, false, Thread.NORM_PRIORITY);
41     }
42
43     public DefaultThreadFactory(String poolName) {
44         this(poolName, false, Thread.NORM_PRIORITY);
45     }
46
47     public DefaultThreadFactory(Class<?> poolType, boolean daemon) {
48         this(poolType, daemon, Thread.NORM_PRIORITY);
49     }
50
51     public DefaultThreadFactory(String poolName, boolean daemon) {
52         this(poolName, daemon, Thread.NORM_PRIORITY);
53     }
54
55     public DefaultThreadFactory(Class<?> poolType, int priority) {
56         this(poolType, false, priority);
57     }
58
59     public DefaultThreadFactory(String poolName, int priority) {
60         this(poolName, false, priority);
61     }
62
63     public DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority) {
64         this(toPoolName(poolType), daemon, priority);
65     }
66
67     public static String toPoolName(Class<?> poolType) {
68         ObjectUtil.checkNotNull(poolType, "poolType");
69
70         String poolName = StringUtil.simpleClassName(poolType);
71         switch (poolName.length()) {
72             case 0:
73                 return "unknown";
74             case 1:
75                 return poolName.toLowerCase(Locale.US);
76             default:
77                 if (Character.isUpperCase(poolName.charAt(0)) && Character.isLowerCase(poolName.charAt(1))) {
78                     return Character.toLowerCase(poolName.charAt(0)) + poolName.substring(1);
79                 } else {
80                     return poolName;
81                 }
82         }
83     }
84
85     public DefaultThreadFactory(String poolName, boolean daemon, int priority, ThreadGroup threadGroup) {
86         ObjectUtil.checkNotNull(poolName, "poolName");
87
88         if (priority < Thread.MIN_PRIORITY || priority > Thread.MAX_PRIORITY) {
89             throw new IllegalArgumentException(
90                     "priority: " + priority + " (expected: Thread.MIN_PRIORITY <= priority <= Thread.MAX_PRIORITY)");
91         }
92
93         prefix = poolName + '-' + poolId.incrementAndGet() + '-';
94         this.daemon = daemon;
95         this.priority = priority;
96         this.threadGroup = threadGroup;
97     }
98
99     public DefaultThreadFactory(String poolName, boolean daemon, int priority) {
100         this(poolName, daemon, priority, System.getSecurityManager() == null ?
101                 Thread.currentThread().getThreadGroup() : System.getSecurityManager().getThreadGroup());
102     }
103
104     @Override
105     public Thread newThread(Runnable r) {
106         Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet());
107         try {
108             if (t.isDaemon() != daemon) {
109                 t.setDaemon(daemon);
110             }
111
112             if (t.getPriority() != priority) {
113                 t.setPriority(priority);
114             }
115         } catch (Exception ignored) {
116             // Doesn't matter even if failed to set.
117         }
118         return t;
119     }
120
121     protected Thread newThread(Runnable r, String name) {
122         return new FastThreadLocalThread(threadGroup, r, name);
123     }
124 }
125