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