View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.concurrent;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import java.util.concurrent.ThreadFactory;
36  import java.util.concurrent.atomic.AtomicInteger;
37  
38  /**
39   * The default thread factory
40   */
41  public final class CustomThreadFactory implements ThreadFactory {
42      private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
43      private final ThreadGroup group;
44      private final AtomicInteger threadNumber = new AtomicInteger(1);
45      private final String namePrefix;
46      private final boolean daemon;
47      private final int priority;
48  
49  
50      public CustomThreadFactory(final String name, final boolean daemon) {
51          this(name, daemon, Thread.NORM_PRIORITY);
52      }
53  
54      public CustomThreadFactory(final String name, final boolean daemon, final int priority) {
55          SecurityManager s = System.getSecurityManager();
56          group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
57          namePrefix = name + POOL_NUMBER.getAndIncrement() + "-thread-";
58          this.daemon = daemon;
59          this.priority = priority;
60      }
61  
62  
63      @Override
64      @SuppressFBWarnings("MDM_THREAD_PRIORITIES")
65      public Thread newThread(final Runnable r) {
66          Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
67          t.setDaemon(daemon);
68          t.setPriority(priority);
69          return t;
70      }
71  
72      @Override
73      public String toString() {
74          return "CustomThreadFactory{" + "group=" + group + ", threadNumber=" + threadNumber
75                  + ", namePrefix=" + namePrefix + ", daemon=" + daemon + ", priority=" + priority + '}';
76      }
77  
78  
79  
80  }