Class NamedThreadFactory

  • All Implemented Interfaces:
    ThreadFactory

    public final class NamedThreadFactory
    extends Object
    implements ThreadFactory
    Creates threads with unique, sequentially numbered names.

    Each thread is assigned a name consisting of a base name followed by a hyphen and an incrementing number, beginning with 1.

    The thread counter is maintained independently for each factory instance and is safe for concurrent use.

    Author:
    Charles B. Kozoyed
    • Constructor Detail

      • NamedThreadFactory

        public NamedThreadFactory​(String baseName)
        Constructs a thread factory using the specified base name.
        Parameters:
        baseName - the base name assigned to threads created by this factory
        Throws:
        IllegalArgumentException - if the base name is null or blank
    • Method Detail

      • newThread

        public Thread newThread​(Runnable runnable)
        Creates a new thread for executing the specified task.

        The thread is assigned a unique name in the format baseName-N, where N is a sequential number starting at 1.

        The returned thread is not started.

        Specified by:
        newThread in interface ThreadFactory
        Parameters:
        runnable - the task to execute in the new thread
        Returns:
        a new, unstarted thread with a sequentially numbered name