View Javadoc
1   package org.apache.maven.surefire.util.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.ThreadFactory;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  /**
26   * Creates new daemon Thread.
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       * Should be used by thread pools.
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 }