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      @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       * Should be used by thread pools.
60       * @return new instance of {@link ThreadFactory} where each {@link Thread thread} is daemon
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 }