1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import java.util.concurrent.ThreadFactory;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23
24
25
26 public class DaemonThreadFactory implements ThreadFactory {
27 static final AtomicInteger poolNumber = new AtomicInteger(1);
28 final ThreadGroup group;
29 final AtomicInteger threadNumber = new AtomicInteger(1);
30 final String namePrefix;
31
32 public DaemonThreadFactory(String name) {
33 SecurityManager s = System.getSecurityManager();
34 group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
35 namePrefix = name + poolNumber.getAndIncrement() + "-thread-";
36 }
37
38 @Override
39 public Thread newThread(Runnable r) {
40 Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
41 if (!t.isDaemon()) {
42 t.setDaemon(true);
43 }
44 if (t.getPriority() != Thread.NORM_PRIORITY) {
45 t.setPriority(Thread.NORM_PRIORITY);
46 }
47 return t;
48 }
49 }