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    @Mutable()
037    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
038    public final class LDAPSDKThreadFactory
039           implements ThreadFactory
040    {
041      // The counter that will be used for the thread number.
042      private final AtomicLong threadCounter;
043    
044      // Indicates whether the threads should be created as daemon threads.
045      private final boolean daemon;
046    
047      // The base name to use for newly-created threads.
048      private final String baseName;
049    
050      // The thread group that should be used for the threads.
051      private final ThreadGroup threadGroup;
052    
053    
054    
055      /**
056       * Creates a new instance of this thread factory with the provided settings.
057       * Threads created will have the default thread group.
058       *
059       * @param  baseName  The base name to use for threads created by this factory.
060       * @param  daemon    Indicates whether the threads should be created as daemon
061       *                   threads.
062       */
063      public LDAPSDKThreadFactory(final String baseName, final boolean daemon)
064      {
065        this(baseName, daemon, null);
066      }
067    
068    
069    
070      /**
071       * Creates a new instance of this thread factory with the provided settings.
072       *
073       * @param  baseName     The base name to use for threads created by this
074       *                      factory.  It must not be {@code null}.
075       * @param  daemon       Indicates whether the threads should be created as
076       *                      daemon threads.
077       * @param  threadGroup  The thread group to use for threads created by this
078       *                      factory.  It may be {@code null} if the default thread
079       *                      group should be used.
080       */
081      public LDAPSDKThreadFactory(final String baseName, final boolean daemon,
082                                  final ThreadGroup threadGroup)
083      {
084        this.baseName     = baseName;
085        this.daemon       = daemon;
086        this.threadGroup  = threadGroup;
087    
088        threadCounter = new AtomicLong(1L);
089      }
090    
091    
092    
093      /**
094       * Creates a new thread using the settings for this thread factory.  The new
095       * thread will not be started.
096       *
097       * @param  r  The {@code Runnable} target that will be used for the actual
098       *            thread logic.  It must not be {@code null}.
099       *
100       * @return  The newly-created (but not yet started) thread.
101       */
102      public Thread newThread(final Runnable r)
103      {
104        final String name = baseName + ' ' + threadCounter.getAndIncrement();
105        final Thread t = new Thread(threadGroup, r, baseName);
106        t.setDaemon(daemon);
107        return t;
108      }
109    }