1
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
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
117 }
118 return t;
119 }
120
121 protected Thread newThread(Runnable r, String name) {
122 return new FastThreadLocalThread(threadGroup, r, name);
123 }
124 }
125