001/*
002 * Copyright 2011-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2011-2022 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-2022 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  @NotNull 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  @NotNull private final String baseName;
064
065  // The thread group that should be used for the threads.
066  @Nullable 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(@NotNull final String baseName,
079                              final boolean daemon)
080  {
081    this(baseName, daemon, null);
082  }
083
084
085
086  /**
087   * Creates a new instance of this thread factory with the provided settings.
088   *
089   * @param  baseName     The base name to use for threads created by this
090   *                      factory.  It must not be {@code null}.
091   * @param  daemon       Indicates whether the threads should be created as
092   *                      daemon threads.
093   * @param  threadGroup  The thread group to use for threads created by this
094   *                      factory.  It may be {@code null} if the default thread
095   *                      group should be used.
096   */
097  public LDAPSDKThreadFactory(@NotNull final String baseName,
098                              final boolean daemon,
099                              @Nullable final ThreadGroup threadGroup)
100  {
101    this.baseName     = baseName;
102    this.daemon       = daemon;
103    this.threadGroup  = threadGroup;
104
105    threadCounter = new AtomicLong(1L);
106  }
107
108
109
110  /**
111   * Creates a new thread using the settings for this thread factory.  The new
112   * thread will not be started.
113   *
114   * @param  r  The {@code Runnable} target that will be used for the actual
115   *            thread logic.  It must not be {@code null}.
116   *
117   * @return  The newly-created (but not yet started) thread.
118   */
119  @Override()
120  @NotNull()
121  public Thread newThread(@NotNull final Runnable r)
122  {
123    final String name = baseName + ' ' + threadCounter.getAndIncrement();
124    final Thread t = new Thread(threadGroup, r, name);
125    t.setDaemon(daemon);
126    return t;
127  }
128}