View Javadoc

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