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    *   https://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.netty5.util.concurrent;
18  
19  import io.netty5.util.internal.StringUtil;
20  
21  import java.util.Locale;
22  import java.util.concurrent.ThreadFactory;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * A {@link ThreadFactory} implementation with a simple naming rule.
29   */
30  public class DefaultThreadFactory implements ThreadFactory {
31  
32      private static final AtomicInteger poolId = new AtomicInteger();
33  
34      private final AtomicInteger nextId = new AtomicInteger();
35      private final String prefix;
36      private final boolean daemon;
37      private final int priority;
38      protected final ThreadGroup threadGroup;
39  
40      public DefaultThreadFactory(Class<?> poolType) {
41          this(poolType, false, Thread.NORM_PRIORITY);
42      }
43  
44      public DefaultThreadFactory(String poolName) {
45          this(poolName, false, Thread.NORM_PRIORITY);
46      }
47  
48      public DefaultThreadFactory(Class<?> poolType, boolean daemon) {
49          this(poolType, daemon, Thread.NORM_PRIORITY);
50      }
51  
52      public DefaultThreadFactory(String poolName, boolean daemon) {
53          this(poolName, daemon, Thread.NORM_PRIORITY);
54      }
55  
56      public DefaultThreadFactory(Class<?> poolType, int priority) {
57          this(poolType, false, priority);
58      }
59  
60      public DefaultThreadFactory(String poolName, int priority) {
61          this(poolName, false, priority);
62      }
63  
64      public DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority) {
65          this(toPoolName(poolType), daemon, priority);
66      }
67  
68      public static String toPoolName(Class<?> poolType) {
69          requireNonNull(poolType, "poolType");
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          requireNonNull(poolName, "poolName");
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, null);
101     }
102 
103     @Override
104     public Thread newThread(Runnable r) {
105         Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet());
106         try {
107             if (t.isDaemon() != daemon) {
108                 t.setDaemon(daemon);
109             }
110 
111             if (t.getPriority() != priority) {
112                 t.setPriority(priority);
113             }
114         } catch (Exception ignored) {
115             // Doesn't matter even if failed to set.
116         }
117         return t;
118     }
119 
120     protected Thread newThread(Runnable r, String name) {
121         return new FastThreadLocalThread(threadGroup, r, name);
122     }
123 }