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 public Thread newThread( Runnable r )
47 {
48 Thread t = new Thread( group, r, namePrefix + threadNumber.getAndIncrement() );
49 if ( t.getPriority() != Thread.NORM_PRIORITY )
50 {
51 t.setPriority( Thread.NORM_PRIORITY );
52 }
53 t.setDaemon( true );
54 return t;
55 }
56
57
58
59
60 public static ThreadFactory newDaemonThreadFactory()
61 {
62 return new DaemonThreadFactory();
63 }
64
65 public static ThreadFactory newDaemonThreadFactory( String name )
66 {
67 return new NamedThreadFactory( name );
68 }
69
70 public static Thread newDaemonThread( Runnable r )
71 {
72 SecurityManager s = System.getSecurityManager();
73 ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
74 Thread t = new Thread( group, r );
75 if ( t.getPriority() != Thread.NORM_PRIORITY )
76 {
77 t.setPriority( Thread.NORM_PRIORITY );
78 }
79 t.setDaemon( true );
80 return t;
81 }
82
83 public static Thread newDaemonThread( Runnable r, String name )
84 {
85 SecurityManager s = System.getSecurityManager();
86 ThreadGroup group = s == null ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
87 Thread t = new Thread( group, r, name );
88 if ( t.getPriority() != Thread.NORM_PRIORITY )
89 {
90 t.setPriority( Thread.NORM_PRIORITY );
91 }
92 t.setDaemon( true );
93 return t;
94 }
95
96 private static class NamedThreadFactory
97 implements ThreadFactory
98 {
99
100 private final String name;
101
102 private NamedThreadFactory( String name )
103 {
104 this.name = name;
105 }
106
107 public Thread newThread( Runnable r )
108 {
109 return newDaemonThread( r, name );
110 }
111 }
112 }