jsr166y.forkjoin
public class ForkJoinWorkerThread extends java.lang.Thread
The form of supported static methods reflects the fact that worker threads may access and process tasks obtained in any of three ways. In preference order: Local tasks are processed in LIFO (newest first) order. Stolen tasks are obtained from other threads in FIFO (oldest first) order, only if there are no local tasks to run. Submissions form a FIFO queue common to the entire pool, and are started only if no other work is available.
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 cleanup methods surrounding the main task processing loop. If you do create such a subclass, you will also need to supply a custom ForkJoinWorkerThreadFactory to use it in a ForkJoinPool.
Modifier | Constructor and Description |
---|---|
protected |
ForkJoinWorkerThread(ForkJoinPool pool)
Creates a ForkJoinWorkerThread operating in the given pool.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
executeLocalTask()
Execute the next task locally queued by the current worker, if
one is available.
|
static boolean |
executeTask()
Helps this program complete by processing a local, stolen or
submitted task, if one is available.
|
static int |
getEstimatedSurplusTaskCount()
Returns an estimate of how many more locally queued tasks there
are than idle worker threads that might steal them.
|
static int |
getLocalQueueSize()
Returns the number of tasks waiting to be run by the current
worker thread.
|
static ForkJoinPool |
getPool()
Returns the pool hosting the current task execution.
|
static int |
getPoolIndex()
Returns the index number of the current worker thread in its
pool.
|
static double |
nextRandomDouble()
Returns a random double using a per-worker random
number generator with the same properties as
Random.nextDouble() |
static int |
nextRandomInt()
Returns a random integer using a per-worker random
number generator with the same properties as
Random.nextInt() |
static int |
nextRandomInt(int n)
Returns a random integer using a per-worker random
number generator with the same properties as
Random.nextInt(int) |
static long |
nextRandomLong()
Returns a random long using a per-worker random
number generator with the same properties as
Random.nextLong() |
static long |
nextRandomLong(long n)
Returns a random integer using a per-worker random
number generator with the same properties as
Random.nextInt(int) |
protected void |
onStart()
Initializes internal state after construction but before
processing any tasks.
|
protected void |
onTermination(java.lang.Throwable exception)
Perform cleanup associated with termination of this worker
thread.
|
static ForkJoinTask<?> |
peekLocalTask()
Returns, but does not remove or execute, the next task locally
queued for execution by the current worker thread.
|
static ForkJoinTask<?> |
pollLocalTask()
Removes and returns, without executing, the next task queued
for execution in the current worker thread's local queue.
|
static ForkJoinTask<?> |
pollTask()
Removes and returns, without executing, the next task available
for execution by the current worker thread, which may be a
locally queued task, one stolen from another worker, or a pool
submission.
|
static boolean |
removeIfNextLocalTask(ForkJoinTask<?> task)
Removes and returns, without executing, the given task from the
queue hosting current execution only if it would be the next
task executed by the current worker.
|
void |
run()
This method is required to be public, but should never be
called explicitly.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
protected ForkJoinWorkerThread(ForkJoinPool pool)
pool
- the pool this thread works injava.lang.NullPointerException
- if pool is null;public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
protected void onStart()
protected void onTermination(java.lang.Throwable exception)
exception
- the exception causing this thread to abort due
to an unrecoverable error, or null if completed normally.public static ForkJoinPool getPool()
public static int getPoolIndex()
public static int getLocalQueueSize()
public static int getEstimatedSurplusTaskCount()
public static ForkJoinTask<?> peekLocalTask()
public static ForkJoinTask<?> pollLocalTask()
public static boolean removeIfNextLocalTask(ForkJoinTask<?> task)
task
- the taskpublic static boolean executeLocalTask()
public static ForkJoinTask<?> pollTask()
public static boolean executeTask()
while (!t1.isDone() && !t2.isDone()) ForkJoinWorkerThread.executeTask();Similarly, you can help process tasks until all computations complete via
while(ForkJoinWorkerThread.executeTask() || !ForkJoinWorkerThread.getPool().isQuiescent()) ;
public static int nextRandomInt()
Random.nextInt()
int
value from this worker's random number generator's sequencepublic static int nextRandomInt(int n)
Random.nextInt(int)
n
- the bound on the random number to be returned. Must be
positive.int
value between 0
(inclusive) and n
(exclusive)
from this worker's random number generator's sequencejava.lang.IllegalArgumentException
- if n is not positivepublic static long nextRandomLong()
Random.nextLong()
long
value from this worker's random number generator's sequencepublic static long nextRandomLong(long n)
Random.nextInt(int)
n
- the bound on the random number to be returned. Must be
positive.int
value between 0
(inclusive) and n
(exclusive)
from this worker's random number generator's sequencejava.lang.IllegalArgumentException
- if n is not positivepublic static double nextRandomDouble()
Random.nextDouble()
double
value between 0.0
and 1.0
from this
worker's random number generator's sequence