View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util.internal;
20  
21  import java.util.concurrent.Executors;
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 implements ThreadFactory {
29      private static final ThreadFactory DEFAULT_THREAD_FACTORY = Executors.defaultThreadFactory();
30  
31      private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
32  
33      private final AtomicInteger threadNumber = new AtomicInteger(1);
34  
35      private final String namePrefix;
36  
37      private DaemonThreadFactory() {
38          namePrefix = "pool-" + POOL_NUMBER.getAndIncrement() + "-thread-";
39      }
40  
41      @Override
42      public Thread newThread(Runnable r) {
43          Thread t = DEFAULT_THREAD_FACTORY.newThread(r);
44          t.setName(namePrefix + threadNumber.getAndIncrement());
45          t.setDaemon(true);
46          return t;
47      }
48  
49      /**
50       * Should be used by thread pools.
51       * @return new instance of {@link ThreadFactory} where each {@link Thread thread} is daemon
52       */
53      public static ThreadFactory newDaemonThreadFactory() {
54          return new DaemonThreadFactory();
55      }
56  
57      public static ThreadFactory newDaemonThreadFactory(String name) {
58          return new NamedThreadFactory(name);
59      }
60  
61      public static Thread newDaemonThread(Runnable r) {
62          return new DaemonThreadFactory().newThread(r);
63      }
64  
65      public static Thread newDaemonThread(Runnable r, String name) {
66          Thread t = new DaemonThreadFactory().newThread(r);
67          t.setName(name);
68          return t;
69      }
70  
71      private static class NamedThreadFactory implements ThreadFactory {
72          private final String name;
73  
74          private NamedThreadFactory(String name) {
75              this.name = name;
76          }
77  
78          @Override
79          public Thread newThread(Runnable r) {
80              return newDaemonThread(r, name);
81          }
82      }
83  }