Class ThreadReferencingThreadFactory

  • All Implemented Interfaces:
    java.util.concurrent.ThreadFactory

    public class ThreadReferencingThreadFactory
    extends ConfigurableThreadFactory
    A thread factory which keeps a WeakReference to each thread. These threads (if still referenced by the VM) can then be retrieved by using getThreads(boolean).
    Since:
    5.15
    • Constructor Detail

      • ThreadReferencingThreadFactory

        public ThreadReferencingThreadFactory()
        Constructs a new ThreadReferencingThreadFactory 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 ThreadReferencingThreadFactory instances.
      • ThreadReferencingThreadFactory

        public ThreadReferencingThreadFactory​(java.lang.String threadNamePrefix,
                                              boolean appendPoolIdToPrefix)
        Constructs a new ThreadReferencingThreadFactory 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
      • ThreadReferencingThreadFactory

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

        public ThreadReferencingThreadFactory​(int threadPriority)
        Constructs a new ThreadReferencingThreadFactory 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
      • ThreadReferencingThreadFactory

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

        public ThreadReferencingThreadFactory​(java.util.function.Consumer<java.lang.Thread> notifyThreadCreation)
        Constructs a new ThreadReferencingThreadFactory 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
      • ThreadReferencingThreadFactory

        public ThreadReferencingThreadFactory​(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 ThreadReferencingThreadFactory 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

      • getThreads

        public java.util.List<java.lang.Thread> getThreads​(boolean requireAlive)
        Get a list of currently known threads. This provides the ability to check if the thread is still alive before it is added, but threads may die / stop while the result is building so inactive threads may still be returned.
        Parameters:
        requireAlive - true to only provide threads currently seen as alive
        Returns:
        A list of known thread references still reachable
      • newThread

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