Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Constants](https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#inhconstants) \| [Protected Ctors](https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#proctors) \| [Methods](https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#pubmethods) \| [Protected Methods](https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#promethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#inhmethods)

# ForkJoinWorkerThread


`
public


class
ForkJoinWorkerThread
`


`


`

`

extends https://developer.android.com/reference/java/lang/Thread


`

`


`

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.lang.Thread](https://developer.android.com/reference/java/lang/Thread) ||
|   | ↳ | java.util.concurrent.ForkJoinWorkerThread |

<br />

*** ** * ** ***

A thread managed by a `https://developer.android.com/reference/java/util/concurrent/ForkJoinPool`, which executes
`https://developer.android.com/reference/java/util/concurrent/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 `https://developer.android.com/reference/java/util/concurrent/ForkJoinPool.ForkJoinWorkerThreadFactory` to
[use it](https://developer.android.com/reference/java/util/concurrent/ForkJoinPool#ForkJoinPool(int,%20java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory,%20java.lang.Thread.UncaughtExceptionHandler,%20boolean,%20int,%20int,%20int,%20java.util.function.Predicate<?%20super%20java.util.concurrent.ForkJoinPool>,%20long,%20java.util.concurrent.TimeUnit)) in a `ForkJoinPool`.

## Summary

| ### Inherited constants |
|---|
| From class `https://developer.android.com/reference/java/lang/Thread` |---|---| | `int` | `https://developer.android.com/reference/java/lang/Thread#MAX_PRIORITY` The maximum priority that a thread can have. | | `int` | `https://developer.android.com/reference/java/lang/Thread#MIN_PRIORITY` The minimum priority that a thread can have. | | `int` | `https://developer.android.com/reference/java/lang/Thread#NORM_PRIORITY` The default priority that is assigned to a thread. | |

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#ForkJoinWorkerThread(java.lang.ThreadGroup,%20java.util.concurrent.ForkJoinPool,%20boolean)(https://developer.android.com/reference/java/lang/ThreadGroup group, https://developer.android.com/reference/java/util/concurrent/ForkJoinPool pool, boolean preserveThreadLocals) ` Creates a ForkJoinWorkerThread operating in the given thread group and pool, and with the given policy for preserving ThreadLocals. |
| ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#ForkJoinWorkerThread(java.util.concurrent.ForkJoinPool)(https://developer.android.com/reference/java/util/concurrent/ForkJoinPool pool) ` Creates a ForkJoinWorkerThread operating in the given pool. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/util/concurrent/ForkJoinPool` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#getPool()() ` Returns the pool hosting this thread. |
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#getPoolIndex()() ` Returns the unique index number of this thread in its pool. |
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#getQueuedTaskCount()() ` |
| ` void` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#run()() ` This method is required to be public, but should never be called explicitly. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#onStart()() ` Initializes internal state after construction but before processing any tasks. |
| ` void` | ` https://developer.android.com/reference/java/util/concurrent/ForkJoinWorkerThread#onTermination(java.lang.Throwable)(https://developer.android.com/reference/java/lang/Throwable exception) ` Performs cleanup associated with termination of this worker thread. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Thread ` |---|---| | ` static int` | ` https://developer.android.com/reference/java/lang/Thread#activeCount()() ` Returns an estimate of the number of active threads in the current thread's [thread group](https://developer.android.com/reference/java/lang/ThreadGroup) and its subgroups. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#checkAccess()() ` *This method was deprecated in API level 36. This method is only useful in conjunction with [the Security Manager](https://developer.android.com/reference/java/lang/SecurityManager), which is deprecated and subject to removal in a future release. Consequently, this method is also deprecated and subject to removal. There is no replacement for the Security Manager or this method.* | | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Thread#clone()() ` Throws CloneNotSupportedException as a Thread can not be meaningfully cloned. | | ` static https://developer.android.com/reference/java/lang/Thread` | ` https://developer.android.com/reference/java/lang/Thread#currentThread()() ` Returns a reference to the currently executing thread object. | | ` static void` | ` https://developer.android.com/reference/java/lang/Thread#dumpStack()() ` Prints a stack trace of the current thread to the standard error stream. | | ` static int` | ` https://developer.android.com/reference/java/lang/Thread#enumerate(java.lang.Thread[])(https://developer.android.com/reference/java/lang/Thread tarray) ` Copies into the specified array every active thread in the current thread's thread group and its subgroups. | | ` static https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/lang/Thread, https://developer.android.com/reference/java/lang/StackTraceElement>` | ` https://developer.android.com/reference/java/lang/Thread#getAllStackTraces()() ` Returns a map of stack traces for all live threads. | | ` https://developer.android.com/reference/java/lang/ClassLoader` | ` https://developer.android.com/reference/java/lang/Thread#getContextClassLoader()() ` Returns the context `ClassLoader` for this thread. | | ` static https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler` | ` https://developer.android.com/reference/java/lang/Thread#getDefaultUncaughtExceptionHandler()() ` Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. | | ` long` | ` https://developer.android.com/reference/java/lang/Thread#getId()() ` *This method was deprecated in API level 36. This method is not final and may be overridden to return a value that is not the thread ID. Use `https://developer.android.com/reference/java/lang/Thread#threadId()` instead.* | | ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Thread#getName()() ` Returns this thread's name. | | ` final int` | ` https://developer.android.com/reference/java/lang/Thread#getPriority()() ` Returns this thread's priority. | | ` https://developer.android.com/reference/java/lang/StackTraceElement` | ` https://developer.android.com/reference/java/lang/Thread#getStackTrace()() ` Returns an array of stack trace elements representing the stack dump of this thread. | | ` https://developer.android.com/reference/java/lang/Thread.State` | ` https://developer.android.com/reference/java/lang/Thread#getState()() ` Returns the state of this thread. | | ` final https://developer.android.com/reference/java/lang/ThreadGroup` | ` https://developer.android.com/reference/java/lang/Thread#getThreadGroup()() ` Returns the thread group to which this thread belongs. | | ` https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler` | ` https://developer.android.com/reference/java/lang/Thread#getUncaughtExceptionHandler()() ` Returns the handler invoked when this thread abruptly terminates due to an uncaught exception. | | ` static boolean` | ` https://developer.android.com/reference/java/lang/Thread#holdsLock(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Returns `true` if and only if the current thread holds the monitor lock on the specified object. | | ` void` | ` https://developer.android.com/reference/java/lang/Thread#interrupt()() ` Interrupts this thread. | | ` static boolean` | ` https://developer.android.com/reference/java/lang/Thread#interrupted()() ` Tests whether the current thread has been interrupted. | | ` final boolean` | ` https://developer.android.com/reference/java/lang/Thread#isAlive()() ` Tests if this thread is alive. | | ` final boolean` | ` https://developer.android.com/reference/java/lang/Thread#isDaemon()() ` Tests if this thread is a daemon thread. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Thread#isInterrupted()() ` Tests whether this thread has been interrupted. | | ` final boolean` | ` https://developer.android.com/reference/java/lang/Thread#isVirtual()() ` Returns `true` if this thread is a virtual thread. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#join()() ` Waits for this thread to die. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#join(long)(long millis) ` Waits at most `millis` milliseconds for this thread to die. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#join(long,%20int)(long millis, int nanos) ` Waits at most `millis` milliseconds plus `nanos` nanoseconds for this thread to die. | | ` final boolean` | ` https://developer.android.com/reference/java/lang/Thread#join(java.time.Duration)(https://developer.android.com/reference/java/time/Duration duration) ` Waits for this thread to terminate for up to the given waiting duration. | | ` static void` | ` https://developer.android.com/reference/java/lang/Thread#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` | ` https://developer.android.com/reference/java/lang/Thread#run()() ` If this thread was constructed using a separate `Runnable` run object, then that `Runnable` object's `run` method is called; otherwise, this method does nothing and returns. | | ` void` | ` https://developer.android.com/reference/java/lang/Thread#setContextClassLoader(java.lang.ClassLoader)(https://developer.android.com/reference/java/lang/ClassLoader cl) ` Sets the context ClassLoader for this Thread. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#setDaemon(boolean)(boolean on) ` Marks this thread as either a [daemon](https://developer.android.com/reference/java/lang/Thread#isDaemon()) thread or a user thread. | | ` static void` | ` https://developer.android.com/reference/java/lang/Thread#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)(https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler eh) ` 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` | ` https://developer.android.com/reference/java/lang/Thread#setName(java.lang.String)(https://developer.android.com/reference/java/lang/String name) ` Changes the name of this thread to be equal to the argument `name`. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#setPriority(int)(int newPriority) ` Changes the priority of this thread. | | ` void` | ` https://developer.android.com/reference/java/lang/Thread#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)(https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler eh) ` Set the handler invoked when this thread abruptly terminates due to an uncaught exception. | | ` static void` | ` https://developer.android.com/reference/java/lang/Thread#sleep(long,%20int)(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` | ` https://developer.android.com/reference/java/lang/Thread#sleep(long)(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` | ` https://developer.android.com/reference/java/lang/Thread#sleep(java.time.Duration)(https://developer.android.com/reference/java/time/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` | ` https://developer.android.com/reference/java/lang/Thread#start()() ` Causes this thread to begin execution; the Java Virtual Machine calls the `run` method of this thread. | | ` final void` | ` https://developer.android.com/reference/java/lang/Thread#stop()() ` *This method was deprecated in API level 15. This method was originally designed to force a thread to stop and throw a `ThreadDeath` as an exception. It was inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked `ThreadDeath` exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of `stop` should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the `interrupt` method should be used to interrupt the wait. For more information, see [Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?](https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html).* | | ` final long` | ` https://developer.android.com/reference/java/lang/Thread#threadId()() ` Returns the identifier of this Thread. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Thread#toString()() ` Returns a string representation of this thread, including the thread's name, priority, and thread group. | | ` static void` | ` https://developer.android.com/reference/java/lang/Thread#yield()() ` A hint to the scheduler that the current thread is willing to yield its current use of a processor. | ||
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(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. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(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` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||
| From interface ` https://developer.android.com/reference/java/lang/Runnable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/Runnable#run()() ` When an object implementing interface `Runnable` is used to create a thread, starting the thread causes the object's `run` method to be called in that separately executing thread. | ||

## Protected constructors

### ForkJoinWorkerThread

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `group` | `ThreadGroup`: if non-null, the thread group for this thread. Otherwise, the thread group is chosen by the security manager if present, else set to the current thread's thread group. <br /> |
| `pool` | `ForkJoinPool`: the pool this thread works in <br /> |
| `preserveThreadLocals` | `boolean`: if true, always preserve the values of ThreadLocal variables across tasks; otherwise they may be cleared. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if pool is null |

### ForkJoinWorkerThread

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected ForkJoinWorkerThread (ForkJoinPool pool)
```

Creates a ForkJoinWorkerThread operating in the given pool.

<br />

| Parameters ||
|---|---|
| `pool` | `ForkJoinPool`: the pool this thread works in <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if pool is null |

## Public methods

### getPool

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ForkJoinPool getPool ()
```

Returns the pool hosting this thread.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/ForkJoinPool` | the pool <br /> |

### getPoolIndex

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `int` | the index number <br /> |

### getQueuedTaskCount

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getQueuedTaskCount ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | a (non-negative) estimate of the number of tasks in the thread's queue <br /> |

**See also:**

- `https://developer.android.com/reference/java/util/concurrent/ForkJoinPool#getQueuedTaskCount()`

### run

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void run ()
```

This method is required to be public, but should never be
called explicitly. It performs the main run loop to execute
`https://developer.android.com/reference/java/util/concurrent/ForkJoinTask`s.

<br />

## Protected methods

### onStart

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

### onTermination

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `exception` | `Throwable`: the exception causing this thread to abort due to an unrecoverable error, or `null` if completed normally <br /> |