1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.concurrent.ThreadFactory;
23 import java.util.concurrent.atomic.AtomicInteger;
24
25
26
27
28 public final class DaemonThreadFactory
29 implements ThreadFactory
30 {
31 private static final AtomicInteger POOL_NUMBER = new AtomicInteger( 1 );
32
33 private final AtomicInteger threadNumber = new AtomicInteger( 1 );
34
35 private final ThreadGroup group;
36
37 private final String namePrefix;
38
39 private DaemonThreadFactory()
40 {
41 SecurityManager s = System.getSecurityManager();
42 group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
43 namePrefix = "pool-" + POOL_NUMBER.getAndIncrement() + "-thread-";
44 }
45
46 @Override
47 public Thread newThread( Runnable r )
48 {
49 Thread t = new Thread( group, r, namePrefix + threadNumber.getAndIncrement() );
50 if ( t.getPriority() != Thread.NORM_PRIORITY )
51 {
52 t.setPriority( Thread.NORM_PRIORITY );
53 }
54 t.setDaemon( true );
55 return t;
56 }
57
58
59
60
61
62 public static ThreadFactory newDaemonThreadFactory()
63 {
64 return new DaemonThreadFactory();
65 }
66
67 public static ThreadFactory newDaemonThreadFactory( String name )
68 {
69 return new NamedThreadFactory( name );
70 }
71
72 public static Thread newDaemonThread( Runnable r )
73 {
74 SecurityManager s = System.getSecurityManager();
75 ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
76 Thread t = new Thread( group, r );
77 if ( t.getPriority() != Thread.NORM_PRIORITY )
78 {
79 t.setPriority( Thread.NORM_PRIORITY );
80 }
81 t.setDaemon( true );
82 return t;
83 }
84
85 public static Thread newDaemonThread( Runnable r, String name )
86 {
87 SecurityManager s = System.getSecurityManager();
88 ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
89 Thread t = new Thread( group, r, name );
90 if ( t.getPriority() != Thread.NORM_PRIORITY )
91 {
92 t.setPriority( Thread.NORM_PRIORITY );
93 }
94 t.setDaemon( true );
95 return t;
96 }
97
98 private static class NamedThreadFactory
99 implements ThreadFactory
100 {
101
102 private final String name;
103
104 private NamedThreadFactory( String name )
105 {
106 this.name = name;
107 }
108
109 @Override
110 public Thread newThread( Runnable r )
111 {
112 return newDaemonThread( r, name );
113 }
114 }
115 }