Class ForkJoinWorkerThread
java.lang.Object
java.lang.Thread
java.util.concurrent.ForkJoinWorkerThread
- All Implemented Interfaces:
Runnable
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.- Since:
- 1.7
-
Nested Class Summary
Nested classes/interfaces declared in class Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandlerModifier and TypeClassDescriptionstatic interfaceA builder forThreadandThreadFactoryobjects.static enumA thread state.static interfaceInterface for handlers invoked when aThreadabruptly terminates due to an uncaught exception. -
Field Summary
Fields declared in class Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITYModifier and TypeFieldDescriptionstatic final intThe maximum priority that a thread can have.static final intThe minimum priority that a thread can have.static final intThe default priority that is assigned to a thread. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedForkJoinWorkerThread(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.protectedCreates a ForkJoinWorkerThread operating in the given pool. -
Method Summary
Modifier and TypeMethodDescriptiongetPool()Returns the pool hosting this thread.intReturns the unique index number of this thread in its pool.intReturns a (non-negative) estimate of the number of tasks in the thread's queue.protected voidonStart()Initializes internal state after construction but before processing any tasks.protected voidonTermination(Throwable exception) Performs cleanup associated with termination of this worker thread.voidrun()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, yieldModifier and TypeMethodDescriptionstatic intReturns an estimate of the number of live platform threads in the current thread's thread group and its subgroups.final voidDeprecated, 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 Objectclone()Throws CloneNotSupportedException as a Thread can not be meaningfully cloned.static ThreadReturns the Thread object for the current thread.static voidPrints a stack trace of the current thread to the standard error stream.static intCopies into the specified array every live platform thread in the current thread's thread group and its subgroups.static Map<Thread, StackTraceElement[]> Returns a map of stack traces for all live platform threads.Returns the contextClassLoaderfor this thread.Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception.longgetId()Deprecated.This method is not final and may be overridden to return a value that is not the thread ID.final StringgetName()Returns this thread's name.final intReturns this thread's priority.Returns an array of stack trace elements representing the stack dump of this thread.getState()Returns the state of this thread.final ThreadGroupReturns the thread's thread group ornullif the thread has terminated.Returns the handler invoked when this thread abruptly terminates due to an uncaught exception.static booleanReturnstrueif and only if the current thread holds the monitor lock on the specified object.voidInterrupts this thread.static booleanTests whether the current thread has been interrupted.final booleanisAlive()Tests if this thread is alive.final booleanisDaemon()Tests if this thread is a daemon thread.booleanTests whether this thread has been interrupted.final booleanReturnstrueif this thread is a virtual thread.final voidjoin()Waits for this thread to terminate.final voidjoin(long millis) Waits at mostmillismilliseconds for this thread to terminate.final voidjoin(long millis, int nanos) Waits at mostmillismilliseconds plusnanosnanoseconds for this thread to terminate.final booleanWaits for this thread to terminate for up to the given waiting duration.static Thread.Builder.OfPlatformReturns a builder for creating a platformThreadorThreadFactorythat creates platform threads.static Thread.Builder.OfVirtualReturns a builder for creating a virtualThreadorThreadFactorythat creates virtual threads.static voidIndicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities.voidSets the contextClassLoaderfor this thread.final voidsetDaemon(boolean on) Marks this thread as either a daemon or non-daemon thread.static voidSet 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 voidChanges the name of this thread to be equal to the argumentname.final voidsetPriority(int newPriority) Changes the priority of this thread.voidSet the handler invoked when this thread abruptly terminates due to an uncaught exception.static voidsleep(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 voidsleep(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 voidCauses the currently executing thread to sleep (temporarily cease execution) for the specified duration, subject to the precision and accuracy of system timers and schedulers.voidstart()Schedules this thread to begin execution.static ThreadstartVirtualThread(Runnable task) Creates a virtual thread to execute a task and schedules it to execute.final longthreadId()Returns the identifier of this Thread.toString()Returns a string representation of this thread.static voidyield()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, waitModifier and TypeMethodDescriptionbooleanIndicates whether some other object is "equal to" this one.protected voidfinalize()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 thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(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 voidwait(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
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 inpreserveThreadLocals- 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
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
-
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:
-
onStart
protected void onStart()Initializes internal state after construction but before processing any tasks. If you override this method, you must invokesuper.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
Performs cleanup associated with termination of this worker thread. If you override this method, you must invokesuper.onTerminationat the end of the overridden method.- Parameters:
exception- the exception causing this thread to abort due to an unrecoverable error, ornullif 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 executeForkJoinTasks.
-