ForkJoinWorkerThread (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.util.concurrent, class: ForkJoinWorkerThread
Keywords: java.util.concurrent.ForkJoinWorkerThread class

# Class ForkJoinWorkerThread

java.lang.Object
java.lang.Thread
java.util.concurrent.ForkJoinWorkerThread

All Implemented Interfaces:
  `Runnable`

public class ForkJoinWorkerThread extends Thread
A thread managed by a `ForkJoinPool`, which executes `ForkJoinTask`s. This class is subclassable solely for the sake of adding functionality -- there are no overridable methods dealing with scheduling or execution. However, you can override initialization and termination methods surrounding the main task processing loop. If you do create such a subclass, you will also need to supply a custom `ForkJoinPool.ForkJoinWorkerThreadFactory` to use it in a `ForkJoinPool`.

Since:
  1.7

- ## Nested Class Summary

  ### Nested classes/interfaces declared in class Thread

  `Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler`
  Modifier and Type
  Class
  Description
  `static interface`
  `Thread.Builder`
  A builder for `Thread` and `ThreadFactory` objects.
  `static enum`
  `Thread.State`
  A thread state.
  `static interface`
  `Thread.UncaughtExceptionHandler`
  Interface for handlers invoked when a `Thread` abruptly terminates due to an uncaught exception.

- ## Field Summary

  ### Fields declared in class Thread

  `MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`
  Modifier and Type
  Field
  Description
  `static final int`
  `MAX_PRIORITY`
  The maximum priority that a thread can have.
  `static final int`
  `MIN_PRIORITY`
  The minimum priority that a thread can have.
  `static final int`
  `NORM_PRIORITY`
  The default priority that is assigned to a thread.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `ForkJoinWorkerThread(ThreadGroup group, ForkJoinPool pool, boolean preserveThreadLocals)`
  Creates a ForkJoinWorkerThread operating in the given thread group and pool, and with the given policy for preserving ThreadLocals.
  `protected`
  `ForkJoinWorkerThread(ForkJoinPool pool)`
  Creates a ForkJoinWorkerThread operating in the given pool.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `ForkJoinPool`
  `getPool()`
  Returns the pool hosting this thread.
  `int`
  `getPoolIndex()`
  Returns the unique index number of this thread in its pool.
  `int`
  `getQueuedTaskCount()`
  Returns a (non-negative) estimate of the number of tasks in the thread's queue.
  `protected void`
  `onStart()`
  Initializes internal state after construction but before processing any tasks.
  `protected void`
  `onTermination(Throwable exception)`
  Performs cleanup associated with termination of this worker thread.
  `void`
  `run()`
  This method is required to be public, but should never be called explicitly.

  ### Methods declared in class Thread

  `activeCount, checkAccess, clone, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, threadId, toString, yield`
  Modifier and Type
  Method
  Description
  `static int`
  `activeCount()`
  Returns an estimate of the number of live platform threads in the current thread's thread group and its subgroups.
  `final void`
  `checkAccess()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  This method originally determined if the currently running thread had permission to modify this thread.
  `protected Object`
  `clone()`
  Throws CloneNotSupportedException as a Thread can not be meaningfully cloned.
  `static Thread`
  `currentThread()`
  Returns the Thread object for the current thread.
  `static void`
  `dumpStack()`
  Prints a stack trace of the current thread to the standard error stream.
  `static int`
  `enumerate(Thread[] tarray)`
  Copies into the specified array every live platform thread in the current thread's thread group and its subgroups.
  `static Map<Thread, StackTraceElement[]>`
  `getAllStackTraces()`
  Returns a map of stack traces for all live platform threads.
  `ClassLoader`
  `getContextClassLoader()`
  Returns the context `ClassLoader` for this thread.
  `static Thread.UncaughtExceptionHandler`
  `getDefaultUncaughtExceptionHandler()`
  Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception.
  `long`
  `getId()`
  Deprecated.
  This method is not final and may be overridden to return a value that is not the thread ID.
  `final String`
  `getName()`
  Returns this thread's name.
  `final int`
  `getPriority()`
  Returns this thread's priority.
  `StackTraceElement[]`
  `getStackTrace()`
  Returns an array of stack trace elements representing the stack dump of this thread.
  `Thread.State`
  `getState()`
  Returns the state of this thread.
  `final ThreadGroup`
  `getThreadGroup()`
  Returns the thread's thread group or `null` if the thread has terminated.
  `Thread.UncaughtExceptionHandler`
  `getUncaughtExceptionHandler()`
  Returns the handler invoked when this thread abruptly terminates due to an uncaught exception.
  `static boolean`
  `holdsLock(Object obj)`
  Returns `true` if and only if the current thread holds the monitor lock on the specified object.
  `void`
  `interrupt()`
  Interrupts this thread.
  `static boolean`
  `interrupted()`
  Tests whether the current thread has been interrupted.
  `final boolean`
  `isAlive()`
  Tests if this thread is alive.
  `final boolean`
  `isDaemon()`
  Tests if this thread is a daemon thread.
  `boolean`
  `isInterrupted()`
  Tests whether this thread has been interrupted.
  `final boolean`
  `isVirtual()`
  Returns `true` if this thread is a virtual thread.
  `final void`
  `join()`
  Waits for this thread to terminate.
  `final void`
  `join(long millis)`
  Waits at most `millis` milliseconds for this thread to terminate.
  `final void`
  `join(long millis, int nanos)`
  Waits at most `millis` milliseconds plus `nanos` nanoseconds for this thread to terminate.
  `final boolean`
  `join(Duration duration)`
  Waits for this thread to terminate for up to the given waiting duration.
  `static Thread.Builder.OfPlatform`
  `ofPlatform()`
  Returns a builder for creating a platform `Thread` or `ThreadFactory` that creates platform threads.
  `static Thread.Builder.OfVirtual`
  `ofVirtual()`
  Returns a builder for creating a virtual `Thread` or `ThreadFactory` that creates virtual threads.
  `static void`
  `onSpinWait()`
  Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities.
  `void`
  `setContextClassLoader(ClassLoader cl)`
  Sets the context `ClassLoader` for this thread.
  `final void`
  `setDaemon(boolean on)`
  Marks this thread as either a daemon or non-daemon thread.
  `static void`
  `setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)`
  Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.
  `final void`
  `setName(String name)`
  Changes the name of this thread to be equal to the argument `name`.
  `final void`
  `setPriority(int newPriority)`
  Changes the priority of this thread.
  `void`
  `setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)`
  Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
  `static void`
  `sleep(long millis)`
  Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
  `static void`
  `sleep(long millis, int nanos)`
  Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.
  `static void`
  `sleep(Duration duration)`
  Causes the currently executing thread to sleep (temporarily cease execution) for the specified duration, subject to the precision and accuracy of system timers and schedulers.
  `void`
  `start()`
  Schedules this thread to begin execution.
  `static Thread`
  `startVirtualThread(Runnable task)`
  Creates a virtual thread to execute a task and schedules it to execute.
  `final long`
  `threadId()`
  Returns the identifier of this Thread.
  `String`
  `toString()`
  Returns a string representation of this thread.
  `static void`
  `yield()`
  A hint to the scheduler that the current thread is willing to yield its current use of a processor.

  ### Methods declared in class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### ForkJoinWorkerThread

    protected ForkJoinWorkerThread(ThreadGroup group, ForkJoinPool pool, boolean preserveThreadLocals)
    Creates a ForkJoinWorkerThread operating in the given thread group and pool, and with the given policy for preserving ThreadLocals.
    Parameters:
      `group` - if non-null, the thread group for this thread. Otherwise, the thread group is set to the current thread's thread group.
      `pool` - the pool this thread works in
      `preserveThreadLocals` - if true, always preserve the values of ThreadLocal variables across tasks; otherwise they may be cleared.
    Throws:
      `NullPointerException` - if pool is null
    Since:
      19

  - ### ForkJoinWorkerThread

    protected ForkJoinWorkerThread(ForkJoinPool pool)
    Creates a ForkJoinWorkerThread operating in the given pool.
    Parameters:
      `pool` - the pool this thread works in
    Throws:
      `NullPointerException` - if pool is null

- ## Method Details

  - ### getPool

    public ForkJoinPool getPool()
    Returns the pool hosting this thread.
    Returns:
      the pool

  - ### getPoolIndex

    public int getPoolIndex()
    Returns the unique index number of this thread in its pool. The returned value ranges from zero to the maximum number of threads (minus one) that may exist in the pool, and does not change during the lifetime of the thread. This method may be useful for applications that track status or collect results per-worker-thread rather than per-task.
    Returns:
      the index number

  - ### getQueuedTaskCount

    public int getQueuedTaskCount()
    Returns a (non-negative) estimate of the number of tasks in the thread's queue.
    Returns:
      a (non-negative) estimate of the number of tasks in the thread's queue
    Since:
      20
    See Also:
      - `ForkJoinPool.getQueuedTaskCount()`

  - ### onStart

    protected void onStart()
    Initializes internal state after construction but before processing any tasks. If you override this method, you must invoke `super.onStart()` at the beginning of the method. Initialization requires care: Most fields must have legal default values, to ensure that attempted accesses from other threads work correctly even before this thread starts processing tasks.

  - ### onTermination

    protected void onTermination(Throwable exception)
    Performs cleanup associated with termination of this worker thread. If you override this method, you must invoke `super.onTermination` at the end of the overridden method.
    Parameters:
      `exception` - the exception causing this thread to abort due to an unrecoverable error, or `null` if completed normally

  - ### run

    public void run()
    This method is required to be public, but should never be called explicitly. It performs the main run loop to execute `ForkJoinTask`s.
    Specified by:
      `run` in interface `Runnable`
    Overrides:
      `run` in class `Thread`