View Javadoc
1   package org.apache.maven.surefire.api.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.Executors;
23  import java.util.concurrent.ThreadFactory;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  /**
27   * Creates new daemon Thread.
28   */
29  public final class DaemonThreadFactory
30      implements ThreadFactory
31  {
32      private static final ThreadFactory DEFAULT_THREAD_FACTORY = Executors.defaultThreadFactory();
33  
34      private static final AtomicInteger POOL_NUMBER = new AtomicInteger( 1 );
35  
36      private final AtomicInteger threadNumber = new AtomicInteger( 1 );
37  
38      private final String namePrefix;
39  
40      private DaemonThreadFactory()
41      {
42          namePrefix = "pool-" + POOL_NUMBER.getAndIncrement() + "-thread-";
43      }
44  
45      @Override
46      public Thread newThread( Runnable r )
47      {
48          Thread t = DEFAULT_THREAD_FACTORY.newThread( r );
49          t.setName( namePrefix + threadNumber.getAndIncrement() );
50          t.setDaemon( true );
51          return t;
52      }
53  
54      /**
55       * Should be used by thread pools.
56       * @return new instance of {@link ThreadFactory} where each {@link Thread thread} is daemon
57       */
58      public static ThreadFactory newDaemonThreadFactory()
59      {
60          return new DaemonThreadFactory();
61      }
62  
63      public static ThreadFactory newDaemonThreadFactory( String name )
64      {
65          return new NamedThreadFactory( name );
66      }
67  
68      public static Thread newDaemonThread( Runnable r )
69      {
70          return new DaemonThreadFactory().newThread( r );
71      }
72  
73      public static Thread newDaemonThread( Runnable r, String name )
74      {
75          Thread t = new DaemonThreadFactory().newThread( r );
76          t.setName( name );
77          return t;
78      }
79  
80      private static class NamedThreadFactory
81          implements ThreadFactory
82      {
83          private final String name;
84  
85          private NamedThreadFactory( String name )
86          {
87              this.name = name;
88          }
89  
90          @Override
91          public Thread newThread( Runnable r )
92          {
93              return newDaemonThread( r, name );
94          }
95      }
96  }