1 package de.desy.acop.displayers.tools;
2
3 import java.util.concurrent.ThreadFactory;
4 import java.util.concurrent.atomic.AtomicInteger;
5
6
7
8
9
10
11
12
13 public class TINEThreadFactory implements ThreadFactory {
14 static final AtomicInteger poolNumber = new AtomicInteger(1);
15 final ThreadGroup group;
16 final AtomicInteger threadNumber = new AtomicInteger(1);
17 final String namePrefix;
18 private boolean lowPriority;
19
20
21
22
23
24
25 public TINEThreadFactory(String name, boolean lowPriority) {
26 SecurityManager s = System.getSecurityManager();
27 group = (s != null)? s.getThreadGroup() :
28 Thread.currentThread().getThreadGroup();
29 namePrefix = name + "-" +
30 poolNumber.getAndIncrement() +
31 "-thread-";
32 this.lowPriority = lowPriority;
33 }
34
35
36
37
38
39 public TINEThreadFactory(String name) {
40 this(name, false);
41 }
42
43
44
45
46
47 public Thread newThread(Runnable r) {
48 Thread t = new Thread(group, r,
49 namePrefix + threadNumber.getAndIncrement(),
50 0);
51 if (t.isDaemon())
52 t.setDaemon(false);
53 if (t.getPriority() != Thread.NORM_PRIORITY)
54 t.setPriority(Thread.NORM_PRIORITY);
55 if (lowPriority) {
56 t.setPriority(Thread.MIN_PRIORITY);
57 }
58 return t;
59 }
60 }