Class ConfigurableThreadFactory

  • All Implemented Interfaces:
    java.util.concurrent.ThreadFactory
    Direct Known Subclasses:
    ThreadReferencingThreadFactory

    public class ConfigurableThreadFactory
    extends java.lang.Object
    implements java.util.concurrent.ThreadFactory
    Implementation of ThreadFactory which is configurable for the most common use cases. Specifically, it allows you several options for how to prefix the name of threads, if returned threads should be daemon or not, what their priority should be, and if an Thread.UncaughtExceptionHandler should be provided to new threads. You can construct this with no arguments, and it's behavior will match that of Executors.defaultThreadFactory().
    Since:
    2.3.0
    • Constructor Detail

      • ConfigurableThreadFactory

        public ConfigurableThreadFactory()
        Constructs a new ConfigurableThreadFactory with the default parameters. Threads produced from this should behave exactly like Executors.defaultThreadFactory(), except the pool number provided in the thread name will be respective to the ones created from other ConfigurableThreadFactory instances.
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(java.lang.String threadNamePrefix,
                                         boolean appendPoolIdToPrefix)
        Constructs a new ConfigurableThreadFactory specifying the prefix for the name of newly created threads.

        If specified with true for appendPoolIdToPrefix it will append a unique "pool" id to the prefix, giving it the format of threadNamePrefix + UNIQUE_POOL_ID + "-thread-". If appendPoolIdToPrefix is specified as false, only a unique thread id will be appended to the prefix. In either case, the produced threads name will be appended with a unique thread id for the factory instance.

        Parameters:
        threadNamePrefix - prefix for all threads created
        appendPoolIdToPrefix - true to append a unique pool id to the thread prefix
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(boolean useDaemonThreads)
        Constructs a new ConfigurableThreadFactory specifying the behavior for if threads should be daemon or not.
        Parameters:
        useDaemonThreads - true if produced threads should be daemon threads
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(int threadPriority)
        Constructs a new ConfigurableThreadFactory specifying the priority for produced threads.

        If the priority is below or above the max available thread priority, this will be adjusted to the limit of the system.

        Parameters:
        threadPriority - Priority for newly created threads
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler)
        Constructs a new ConfigurableThreadFactory specifying an Thread.UncaughtExceptionHandler that will be provided to all newly created threads.
        Parameters:
        defaultUncaughtExceptionHandler - Thread.UncaughtExceptionHandler to provide to newly created threads
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
        Constructs a new ConfigurableThreadFactory specifying a Consumer that will be provided threads as they created.
        Parameters:
        notifyThreadCreation - Consumer to be provided whenever a new thread is about to be returned or null
      • ConfigurableThreadFactory

        public ConfigurableThreadFactory​(java.lang.String threadNamePrefix,
                                         boolean appendPoolIdToPrefix,
                                         boolean useDaemonThreads,
                                         int threadPriority,
                                         java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
                                         ExceptionHandler defaultThreadlyExceptionHandler,
                                         java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
        Constructs a new ConfigurableThreadFactory allowing you to provide specific values for everything which this class allows to be configured. You must use this constructor if you need to adjust two or more values.

        If specified with true for appendPoolIdToPrefix it will append a unique "pool" id to the prefix, giving it the format of threadNamePrefix + UNIQUE_POOL_ID + "-thread-". If appendPoolIdToPrefix is specified as false, only a unique thread id will be appended to the prefix. In either case, the produced threads name will be appended with a unique thread id for the factory instance.

        If the priority is below or above the max available thread priority, this will be adjusted to the limit of the system.

        Parameters:
        threadNamePrefix - prefix for all threads created, null to match default
        appendPoolIdToPrefix - true to append a unique pool id to the thread prefix, true to match default
        useDaemonThreads - true if produced threads should be daemon threads, false to match default
        threadPriority - Priority for newly created threads, Thread.NORM_PRIORITY to match default
        uncaughtExceptionHandler - UncaughtExceptionHandler to provide to newly created threads, null to match default
        defaultThreadlyExceptionHandler - ExceptionHandler to provide to newly created threads
        notifyThreadCreation - Consumer to be provided whenever a new thread is about to be returned or null
    • Method Detail

      • builder

        public static ConfigurableThreadFactory.ConfigurableThreadFactoryBuilder builder()
        Construct a new builder as an alternative to the large full parameter constructor when multiple features are needing to be configured.

        Defaults (equivalent to ConfigurableThreadFactory()):

        • threadNamePrefix: "pool-"
        • appendPoolIdToPrefix: true
        • useDaemonThreads: false
        • threadPriority: Thread.NORM_PRIORITY
        • threadlyExceptionHandler: null
        Returns:
        A new builder which can be configured then finally constructed using build()
        Since:
        5.39
      • newThread

        public java.lang.Thread newThread​(java.lang.Runnable r)
        Specified by:
        newThread in interface java.util.concurrent.ThreadFactory