class ForkJoinPool extends AbstractExecutorService
An ExecutorService
for running ForkJoinTask
s.
A ForkJoinPool
provides the entry point for submissions
from non-ForkJoinTask
clients, as well as management and
monitoring operations.
A ForkJoinPool
differs from other kinds of ExecutorService
mainly by virtue of employing
work-stealing: all threads in the pool attempt to find and
execute tasks submitted to the pool and/or created by other active
tasks (eventually blocking waiting for work if none exist). This
enables efficient processing when most tasks spawn other subtasks
(as do most ForkJoinTask
s), as well as when many small
tasks are submitted to the pool from external clients. Especially
when setting asyncMode to true in constructors,
ForkJoinPool
s may also be appropriate for use with event-style
tasks that are never joined.
A static #commonPool()
is available and appropriate for
most applications. The common pool is used by any ForkJoinTask that
is not explicitly submitted to a specified pool. Using the common
pool normally reduces resource usage (its threads are slowly
reclaimed during periods of non-use, and reinstated upon subsequent
use).
For applications that require separate or custom pools, a
ForkJoinPool
may be constructed with a given target parallelism
level; by default, equal to the number of available processors. The
pool attempts to maintain enough active (or available) threads by
dynamically adding, suspending, or resuming internal worker
threads, even if some tasks are stalled waiting to join
others. However, no such adjustments are guaranteed in the face of
blocked I/O or other unmanaged synchronization. The nested ManagedBlocker
interface enables extension of the kinds of
synchronization accommodated.
In addition to execution and lifecycle control methods, this
class provides status check methods (for example
#getStealCount
) that are intended to aid in developing,
tuning, and monitoring fork/join applications. Also, method
#toString
returns indications of pool state in a
convenient form for informal monitoring.
As is the case with other ExecutorServices, there are three
main task execution methods summarized in the following table.
These are designed to be used primarily by clients not already
engaged in fork/join computations in the current pool. The main
forms of these methods accept instances of ForkJoinTask
,
but overloaded forms also allow mixed execution of plain
Runnable
- or Callable
- based activities as well. However,
tasks that are already executing in a pool should normally instead
use the within-computation forms listed in the table unless using
async event-style tasks that are not usually joined, in which case
there is little difference among choice of methods.
Call from non-fork/join clients | Call from within fork/join computations | |
Arrange async execution | `[[#execute(ForkJoinTask)]]` | `[[ForkJoinTask#fork]]` |
Await and obtain result | `[[#invoke(ForkJoinTask)]]` | `[[ForkJoinTask#invoke]]` |
Arrange exec and obtain Future | `[[#submit(ForkJoinTask)]]` | `[[ForkJoinTask#fork]]` (ForkJoinTasks are Futures) |
java.util.concurrent.ForkJoinPool.common
: parallelism
--
an integer greater than zero, threadFactory
-- the class
name of a `[[ForkJoinWorkerThreadFactory]]`, and
exceptionHandler
-- the class name of a `[[
java.lang.Thread.UncaughtExceptionHandler
Thread.UncaughtExceptionHandler]]`. Upon any error in establishing
these settings, default parameters are used.
Implementation notes: This implementation restricts the
maximum number of running threads to 32767. Attempts to create
pools with greater than the maximum number result in
IllegalArgumentException
.
This implementation rejects submitted tasks (that is, by throwing
`[[RejectedExecutionException]]`) only when the pool is shut down
or internal resources have been exhausted.
- Source
- ForkJoinPool.java
- Since
1.7
- Alphabetic
- By Inheritance
- ForkJoinPool
- AbstractExecutorService
- ExecutorService
- Executor
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
-
new
ForkJoinPool(parallelism: Int, factory: ForkJoinWorkerThreadFactory, handler: UncaughtExceptionHandler, asyncMode: Boolean)
Creates a
ForkJoinPool
with the given parameters.Creates a
ForkJoinPool
with the given parameters.- parallelism
the parallelism level. For default value, use
java.lang.Runtime#availableProcessors
.- factory
the factory for creating new threads. For default value, use
#defaultForkJoinWorkerThreadFactory
.- handler
the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks. For default value, use
null
.- asyncMode
if true, establishes local first-in-first-out scheduling mode for forked tasks that are never joined. This mode may be more appropriate than default locally stack-based mode in applications in which worker threads only process event-style asynchronous tasks. For default value, use
false
.
- Exceptions thrown
IllegalArgumentException
if parallelism less than or equal to zero, or greater than implementation limitNullPointerException
if the factory is nullSecurityException
if a security manager exists and the caller is not permitted to modify threads because it does not holdjava.lang.RuntimePermission
("modifyThread")
-
new
ForkJoinPool(parallelism: Int)
Creates a
ForkJoinPool
with the indicated parallelism level, the #defaultForkJoinWorkerThreadFactory default thread factory, no UncaughtExceptionHandler, and non-async LIFO processing mode.Creates a
ForkJoinPool
with the indicated parallelism level, the #defaultForkJoinWorkerThreadFactory default thread factory, no UncaughtExceptionHandler, and non-async LIFO processing mode.- parallelism
the parallelism level
- Exceptions thrown
IllegalArgumentException
if parallelism less than or equal to zero, or greater than implementation limitSecurityException
if a security manager exists and the caller is not permitted to modify threads because it does not holdjava.lang.RuntimePermission
("modifyThread")
- new ForkJoinPool()
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
def
+(other: String): String
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to any2stringadd[ForkJoinPool] performed by method any2stringadd in scala.Predef.
- Definition Classes
- any2stringadd
-
def
->[B](y: B): (ForkJoinPool, B)
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to ArrowAssoc[ForkJoinPool] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @inline()
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
awaitQuiescence(timeout: Long, unit: TimeUnit): Boolean
If called by a ForkJoinTask operating in this pool, equivalent in effect to
ForkJoinTask#helpQuiesce
.If called by a ForkJoinTask operating in this pool, equivalent in effect to
ForkJoinTask#helpQuiesce
. Otherwise, waits and/or attempts to assist performing tasks until this pool#isQuiescent
or the indicated timeout elapses.- timeout
the maximum time to wait
- unit
the time unit of the timeout argument
- returns
true
if quiescent;false
if the timeout elapsed.
-
def
awaitTermination(timeout: Long, unit: TimeUnit): Boolean
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. Because the
#commonPool()
never terminates until program shutdown, when applied to the common pool, this method is equivalent to#awaitQuiescence
but always returnsfalse
.- timeout
the maximum time to wait
- unit
the time unit of the timeout argument
- returns
true
if this executor terminated andfalse
if the timeout elapsed before termination
- Definition Classes
- ForkJoinPool → ExecutorService
- Exceptions thrown
InterruptedException
if interrupted while waiting
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate() @throws( ... )
-
def
drainTasksTo(c: Collection[_ >: ForkJoinTask[_]]): Int
Removes all available unexecuted submitted and forked tasks from scheduling queues and adds them to the given collection, without altering their execution status.
Removes all available unexecuted submitted and forked tasks from scheduling queues and adds them to the given collection, without altering their execution status. These may include artificially generated or wrapped tasks. This method is designed to be invoked only when the pool is known to be quiescent. Invocations at other times may not remove all tasks. A failure encountered while attempting to add elements to collection
c
may result in elements being in neither, either or both collections when the associated exception is thrown. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.- c
the collection to transfer elements into
- returns
the number of elements transferred
- Attributes
- protected[akka.dispatch.forkjoin]
-
def
ensuring(cond: (ForkJoinPool) ⇒ Boolean, msg: ⇒ Any): ForkJoinPool
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to Ensuring[ForkJoinPool] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: (ForkJoinPool) ⇒ Boolean): ForkJoinPool
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to Ensuring[ForkJoinPool] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: Boolean, msg: ⇒ Any): ForkJoinPool
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to Ensuring[ForkJoinPool] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: Boolean): ForkJoinPool
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to Ensuring[ForkJoinPool] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
execute(task: Runnable): Unit
- Definition Classes
- ForkJoinPool → Executor
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
execute(task: ForkJoinTask[_]): Unit
Arranges for (asynchronous) execution of the given task.
Arranges for (asynchronous) execution of the given task.
- task
the task
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to StringFormat[ForkJoinPool] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @inline()
-
def
getActiveThreadCount(): Int
Returns an estimate of the number of threads that are currently stealing or executing tasks.
Returns an estimate of the number of threads that are currently stealing or executing tasks. This method may overestimate the number of active threads.
- returns
the number of active threads
-
def
getAsyncMode(): Boolean
Returns
true
if this pool uses local first-in-first-out scheduling mode for forked tasks that are never joined.Returns
true
if this pool uses local first-in-first-out scheduling mode for forked tasks that are never joined.- returns
true
if this pool uses async mode
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
def
getFactory(): ForkJoinWorkerThreadFactory
Returns the factory used for constructing new workers.
Returns the factory used for constructing new workers.
- returns
the factory used for constructing new workers
-
def
getParallelism(): Int
Returns the targeted parallelism level of this pool.
Returns the targeted parallelism level of this pool.
- returns
the targeted parallelism level of this pool
-
def
getPoolSize(): Int
Returns the number of worker threads that have started but not yet terminated.
Returns the number of worker threads that have started but not yet terminated. The result returned by this method may differ from
#getParallelism
when threads are created to maintain parallelism when others are cooperatively blocked.- returns
the number of worker threads
-
def
getQueuedSubmissionCount(): Int
Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing.
Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing. This method may take time proportional to the number of submissions.
- returns
the number of queued submissions
-
def
getQueuedTaskCount(): Long
Returns an estimate of the total number of tasks currently held in queues by worker threads (but not including tasks submitted to the pool that have not begun executing).
Returns an estimate of the total number of tasks currently held in queues by worker threads (but not including tasks submitted to the pool that have not begun executing). This value is only an approximation, obtained by iterating across all threads in the pool. This method may be useful for tuning task granularities.
- returns
the number of queued tasks
-
def
getRunningThreadCount(): Int
Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization.
Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization. This method may overestimate the number of running threads.
- returns
the number of worker threads
-
def
getStealCount(): Long
Returns an estimate of the total number of tasks stolen from one thread's work queue by another.
Returns an estimate of the total number of tasks stolen from one thread's work queue by another. The reported value underestimates the actual total number of steals when the pool is not quiescent. This value may be useful for monitoring and tuning fork/join programs: in general, steal counts should be high enough to keep threads busy, but low enough to avoid overhead and contention across threads.
- returns
the number of steals
-
def
getUncaughtExceptionHandler(): UncaughtExceptionHandler
Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.
Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.
- returns
the handler, or
null
if none
-
def
hasQueuedSubmissions(): Boolean
Returns
true
if there are any tasks submitted to this pool that have not yet begun executing.Returns
true
if there are any tasks submitted to this pool that have not yet begun executing.- returns
true
if there are any queued submissions
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
def
invoke[T](task: ForkJoinTask[T]): T
Performs the given task, returning its result upon completion.
Performs the given task, returning its result upon completion. If the computation encounters an unchecked Exception or Error, it is rethrown as the outcome of this invocation. Rethrown exceptions behave in the same way as regular exceptions, but, when possible, contain stack traces (as displayed for example using
ex.printStackTrace()
) of both the current thread as well as the thread actually encountering the exception; minimally only the latter.- task
the task
- returns
the task's result
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
invokeAll[T](tasks: Collection[_ <: Callable[T]]): List[Future[T]]
- Definition Classes
- ForkJoinPool → AbstractExecutorService → ExecutorService
- Exceptions thrown
NullPointerException
<invalid inheritdoc annotation>RejectedExecutionException
<invalid inheritdoc annotation>
-
def
invokeAll[T](arg0: Collection[_ <: Callable[T]], arg1: Long, arg2: TimeUnit): List[Future[T]]
- Definition Classes
- AbstractExecutorService → ExecutorService
- Annotations
- @throws( ... )
-
def
invokeAny[T](arg0: Collection[_ <: Callable[T]], arg1: Long, arg2: TimeUnit): T
- Definition Classes
- AbstractExecutorService → ExecutorService
- Annotations
- @throws( ... ) @throws( ... ) @throws( ... )
-
def
invokeAny[T](arg0: Collection[_ <: Callable[T]]): T
- Definition Classes
- AbstractExecutorService → ExecutorService
- Annotations
- @throws( ... ) @throws( ... )
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
isQuiescent(): Boolean
Returns
true
if all worker threads are currently idle.Returns
true
if all worker threads are currently idle. An idle worker is one that cannot obtain a task to execute because none are available to steal from other threads, and there are no pending submissions to the pool. This method is conservative; it might not returntrue
immediately upon idleness of all threads, but will eventually become true if threads remain inactive.- returns
true
if all threads are currently idle
-
def
isShutdown(): Boolean
Returns
true
if this pool has been shut down.Returns
true
if this pool has been shut down.- returns
true
if this pool has been shut down
- Definition Classes
- ForkJoinPool → ExecutorService
-
def
isTerminated(): Boolean
Returns
true
if all tasks have completed following shut down.Returns
true
if all tasks have completed following shut down.- returns
true
if all tasks have completed following shut down
- Definition Classes
- ForkJoinPool → ExecutorService
-
def
isTerminating(): Boolean
Returns
true
if the process of termination has commenced but not yet completed.Returns
true
if the process of termination has commenced but not yet completed. This method may be useful for debugging. A return oftrue
reported a sufficient period after shutdown may indicate that submitted tasks have ignored or suppressed interruption, or are waiting for I/O, causing this executor not to properly terminate. (See the advisory notes for classForkJoinTask
stating that tasks should not normally entail blocking operations. But if they do, they must abort them on interrupt.)- returns
true
if terminating but not yet terminated
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
newTaskFor[T](callable: Callable[T]): RunnableFuture[T]
- Attributes
- protected[akka.dispatch.forkjoin]
- Definition Classes
- ForkJoinPool → AbstractExecutorService
-
def
newTaskFor[T](runnable: Runnable, value: T): RunnableFuture[T]
- Attributes
- protected[akka.dispatch.forkjoin]
- Definition Classes
- ForkJoinPool → AbstractExecutorService
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
def
pollSubmission(): ForkJoinTask[_]
Removes and returns the next unexecuted submission if one is available.
Removes and returns the next unexecuted submission if one is available. This method may be useful in extensions to this class that re-assign work in systems with multiple pools.
- returns
the next submission, or
null
if none
- Attributes
- protected[akka.dispatch.forkjoin]
-
def
shutdown(): Unit
Possibly initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
Possibly initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no effect on execution state if this is the
#commonPool()
, and no additional effect if already shut down. Tasks that are in the process of being submitted concurrently during the course of this method may or may not be rejected.- Definition Classes
- ForkJoinPool → ExecutorService
- Exceptions thrown
SecurityException
if a security manager exists and the caller is not permitted to modify threads because it does not holdjava.lang.RuntimePermission
("modifyThread")
-
def
shutdownNow(): List[Runnable]
Possibly attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks.
Possibly attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks. Invocation has no effect on execution state if this is the
#commonPool()
, and no additional effect if already shut down. Otherwise, tasks that are in the process of being submitted or executed concurrently during the course of this method may or may not be rejected. This method cancels both existing and unexecuted tasks, in order to permit termination in the presence of task dependencies. So the method always returns an empty list (unlike the case for some other Executors).- returns
an empty list
- Definition Classes
- ForkJoinPool → ExecutorService
- Exceptions thrown
SecurityException
if a security manager exists and the caller is not permitted to modify threads because it does not holdjava.lang.RuntimePermission
("modifyThread")
-
def
submit(task: Runnable): ForkJoinTask[_]
- Definition Classes
- ForkJoinPool → AbstractExecutorService → ExecutorService
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
submit[T](task: Runnable, result: T): ForkJoinTask[T]
- Definition Classes
- ForkJoinPool → AbstractExecutorService → ExecutorService
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
submit[T](task: Callable[T]): ForkJoinTask[T]
- Definition Classes
- ForkJoinPool → AbstractExecutorService → ExecutorService
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
def
submit[T](task: ForkJoinTask[T]): ForkJoinTask[T]
Submits a ForkJoinTask for execution.
Submits a ForkJoinTask for execution.
- task
the task to submit
- returns
the task
- Exceptions thrown
NullPointerException
if the task is nullRejectedExecutionException
if the task cannot be scheduled for execution
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
Returns a string identifying this pool, as well as its state, including indications of run state, parallelism level, and worker and task counts.
Returns a string identifying this pool, as well as its state, including indications of run state, parallelism level, and worker and task counts.
- returns
a string identifying this pool, as well as its state
- Definition Classes
- ForkJoinPool → AnyRef → Any
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
→[B](y: B): (ForkJoinPool, B)
- Implicit
- This member is added by an implicit conversion from ForkJoinPool to ArrowAssoc[ForkJoinPool] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc