APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class ForkJoinWorkerThread

extends Thread
Class Inheritance
Known Direct Subclasses
java.util.concurrent.ForkJoinWorkerThread.InnocuousForkJoinWorkerThread, jdk.internal.misc.CarrierThread
Imports
jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.misc.Unsafe

A thread managed by a ForkJoinPool, which executes ForkJoinTasks. 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.
Author
Doug Lea
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ForkJoinWorkerThread.InnocuousForkJoinWorkerThread

A worker thread that is not a member of any user-defined ThreadGroup, uses the system class loader as thread context class loader, and clears all ThreadLocals after running each top-level task.

Field Summary

Modifier and TypeField and Description
private static final long
private static final JavaLangAccess
pack-priv final ForkJoinPool
private static final long
private static final Unsafe
U

pack-priv final ForkJoinPool.WorkQueue
Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
pack-priv
ForkJoinWorkerThread(ThreadGroup group, ForkJoinPool pool, boolean useSystemClassLoader, boolean clearThreadLocals)

Full nonpublic constructor.

protected
ForkJoinWorkerThread(ThreadGroup
if non-null, the thread group for this thread. Otherwise, the thread group is set to the current thread's thread group.
group
,
ForkJoinPool
the pool this thread works in
pool
,
boolean
if true, always preserve the values of ThreadLocal variables across tasks; otherwise they may be cleared.
preserveThreadLocals
)

Creates a ForkJoinWorkerThread operating in the given thread group and pool, and with the given policy for preserving ThreadLocals.

protected
ForkJoinWorkerThread(ForkJoinPool
the pool this thread works in
pool
)

Creates a ForkJoinWorkerThread operating in the given pool.

Method Summary

Modifier and TypeMethod and Description
public ForkJoinPool

Returns:

the pool
getPool
()

Returns the pool hosting this thread.

public int

Returns:

the index number
getPoolIndex
()

Returns the unique index number of this thread in its pool.

public int

Returns:

a (non-negative) estimate of the number of tasks in the thread's queue
getQueuedTaskCount
()

Returns a (non-negative) estimate of the number of tasks in the thread's queue.

pack-priv static boolean

Returns:

true if the current task is being executed by a worker that has queued work
hasKnownQueuedWork
()

Returns true if the current task is being executed by a ForkJoinWorkerThread that is momentarily known to have one or more queued tasks that it could execute immediately.

protected void
onStart()

Initializes internal state after construction but before processing any tasks.

protected void
onTermination(Throwable
the exception causing this thread to abort due to an unrecoverable error, or null if completed normally
exception
)

Performs cleanup associated with termination of this worker thread.

pack-priv void
onThreadLocalReset()

Performs any further cleanup after ThreadLocals are cleared in method resetThreadLocals

pack-priv final void
resetThreadLocals()

Clears ThreadLocals

public void
run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

This method is required to be public, but should never be called explicitly.

Inherited from java.lang.Thread:
activeCountcheckAccessclonecurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitsetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadthreadIdtoStringyield

Field Detail

INHERITABLETHREADLOCALSback to summary
private static final long INHERITABLETHREADLOCALS
JLAback to summary
private static final JavaLangAccess JLA
poolback to summary
pack-priv final ForkJoinPool pool
THREADLOCALSback to summary
private static final long THREADLOCALS
Uback to summary
private static final Unsafe U
workQueueback to summary
pack-priv final ForkJoinPool.WorkQueue workQueue

Constructor Detail

ForkJoinWorkerThreadback to summary
pack-priv ForkJoinWorkerThread(ThreadGroup group, ForkJoinPool pool, boolean useSystemClassLoader, boolean clearThreadLocals)

Full nonpublic constructor.

ForkJoinWorkerThreadback to summary
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:ThreadGroup

if non-null, the thread group for this thread. Otherwise, the thread group is set to the current thread's thread group.

pool:ForkJoinPool

the pool this thread works in

preserveThreadLocals:boolean

if true, always preserve the values of ThreadLocal variables across tasks; otherwise they may be cleared.

Annotations
@SuppressWarnings:this-escape
Exceptions
NullPointerException:
if pool is null
Since
19
ForkJoinWorkerThreadback to summary
protected ForkJoinWorkerThread(ForkJoinPool pool)

Creates a ForkJoinWorkerThread operating in the given pool.

Parameters
pool:ForkJoinPool

the pool this thread works in

Annotations
@SuppressWarnings:this-escape
Exceptions
NullPointerException:
if pool is null

Method Detail

getPoolback to summary
public ForkJoinPool getPool()

Returns the pool hosting this thread.

Returns:ForkJoinPool

the pool

getPoolIndexback to summary
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:int

the index number

getQueuedTaskCountback to summary
public int getQueuedTaskCount()

Returns a (non-negative) estimate of the number of tasks in the thread's queue.

Returns:int

a (non-negative) estimate of the number of tasks in the thread's queue

Since
20
See Also
ForkJoinPool#getQueuedTaskCount()
hasKnownQueuedWorkback to summary
pack-priv static boolean hasKnownQueuedWork()

Returns true if the current task is being executed by a ForkJoinWorkerThread that is momentarily known to have one or more queued tasks that it could execute immediately. This method is approximate and useful only as a heuristic indicator within a running task.

Returns:boolean

true if the current task is being executed by a worker that has queued work

onStartback to summary
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.

onTerminationback to summary
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:Throwable

the exception causing this thread to abort due to an unrecoverable error, or null if completed normally

onThreadLocalResetback to summary
pack-priv void onThreadLocalReset()

Performs any further cleanup after ThreadLocals are cleared in method resetThreadLocals

resetThreadLocalsback to summary
pack-priv final void resetThreadLocals()

Clears ThreadLocals

runback to summary
public void run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

This method is required to be public, but should never be called explicitly. It performs the main run loop to execute ForkJoinTasks.

java.util.concurrent back to summary

pack-priv final Class ForkJoinWorkerThread.InnocuousForkJoinWorkerThread

extends ForkJoinWorkerThread
Class Inheritance

A worker thread that is not a member of any user-defined ThreadGroup, uses the system class loader as thread context class loader, and clears all ThreadLocals after running each top-level task.

Field Summary

Modifier and TypeField and Description
private static final ThreadGroup
innocuousThreadGroup

The ThreadGroup for all InnocuousForkJoinWorkerThreads

private boolean
Inherited from java.util.concurrent.ForkJoinWorkerThread:
poolworkQueue

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv static ThreadGroup
pack-priv final void
onThreadLocalReset()

Overrides java.util.concurrent.ForkJoinWorkerThread.onThreadLocalReset.

Performs any further cleanup after ThreadLocals are cleared in method resetThreadLocals

public void
setContextClassLoader(ClassLoader cl)

Overrides java.lang.Thread.setContextClassLoader.

Sets the context ClassLoader for this thread.

public void
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler x)

Overrides java.lang.Thread.setUncaughtExceptionHandler.

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Inherited from java.util.concurrent.ForkJoinWorkerThread:
getPoolgetPoolIndexgetQueuedTaskCounthasKnownQueuedWorkonStartonTerminationresetThreadLocalsrun

Field Detail

innocuousThreadGroupback to summary
private static final ThreadGroup innocuousThreadGroup

The ThreadGroup for all InnocuousForkJoinWorkerThreads

resetCCLback to summary
private boolean resetCCL

Constructor Detail

InnocuousForkJoinWorkerThreadback to summary
pack-priv InnocuousForkJoinWorkerThread(ForkJoinPool pool)

Method Detail

createGroupback to summary
pack-priv static ThreadGroup createGroup()
onThreadLocalResetback to summary
pack-priv final void onThreadLocalReset()

Overrides java.util.concurrent.ForkJoinWorkerThread.onThreadLocalReset.

Doc from java.util.concurrent.ForkJoinWorkerThread.onThreadLocalReset.

Performs any further cleanup after ThreadLocals are cleared in method resetThreadLocals

Annotations
@Override
setContextClassLoaderback to summary
public void setContextClassLoader(ClassLoader cl)

Overrides java.lang.Thread.setContextClassLoader.

Doc from java.lang.Thread.setContextClassLoader.

Sets the context ClassLoader for this thread.

The context ClassLoader may be set by the creator of the thread for use by code running in this thread when loading classes and resources.

Annotations
@Override
setUncaughtExceptionHandlerback to summary
public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler x)

Overrides java.lang.Thread.setUncaughtExceptionHandler.

Doc from java.lang.Thread.setUncaughtExceptionHandler.

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

A thread can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set. If no such handler is set then the thread's ThreadGroup object acts as its handler.

Annotations
@Override