001 /*
002 * Copyright 2011-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2011-2016 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.util;
022
023
024
025 import java.util.concurrent.ThreadFactory;
026 import java.util.concurrent.atomic.AtomicLong;
027
028
029
030 /**
031 * This class provides a thread factory implementation that may be used to
032 * create threads with a number of basic settings. The name of each thread will
033 * be followed by a counter indicating the order in which the thread was
034 * created.
035 */
036 public final class LDAPSDKThreadFactory
037 implements ThreadFactory
038 {
039 // The counter that will be used for the thread number.
040 private final AtomicLong threadCounter;
041
042 // Indicates whether the threads should be created as daemon threads.
043 private final boolean daemon;
044
045 // The base name to use for newly-created threads.
046 private final String baseName;
047
048 // The thread group that should be used for the threads.
049 private final ThreadGroup threadGroup;
050
051
052
053 /**
054 * Creates a new instance of this thread factory with the provided settings.
055 * Threads created will have the default thread group.
056 *
057 * @param baseName The base name to use for threads created by this factory.
058 * @param daemon Indicates whether the threads should be created as daemon
059 * threads.
060 */
061 public LDAPSDKThreadFactory(final String baseName, final boolean daemon)
062 {
063 this(baseName, daemon, null);
064 }
065
066
067
068 /**
069 * Creates a new instance of this thread factory with the provided settings.
070 *
071 * @param baseName The base name to use for threads created by this
072 * factory. It must not be {@code null}.
073 * @param daemon Indicates whether the threads should be created as
074 * daemon threads.
075 * @param threadGroup The thread group to use for threads created by this
076 * factory. It may be {@code null} if the default thread
077 * group should be used.
078 */
079 public LDAPSDKThreadFactory(final String baseName, final boolean daemon,
080 final ThreadGroup threadGroup)
081 {
082 this.baseName = baseName;
083 this.daemon = daemon;
084 this.threadGroup = threadGroup;
085
086 threadCounter = new AtomicLong(1L);
087 }
088
089
090
091 /**
092 * Creates a new thread using the settings for this thread factory. The new
093 * thread will not be started.
094 *
095 * @param r The {@code Runnable} target that will be used for the actual
096 * thread logic. It must not be {@code null}.
097 *
098 * @return The newly-created (but not yet started) thread.
099 */
100 public Thread newThread(final Runnable r)
101 {
102 final String name = baseName + ' ' + threadCounter.getAndIncrement();
103 final Thread t = new Thread(threadGroup, r, baseName);
104 t.setDaemon(daemon);
105 return t;
106 }
107 }