ForkJoinPool Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
An ExecutorService
for running ForkJoinTask
s.
[Android.Runtime.Register("java/util/concurrent/ForkJoinPool", DoNotGenerateAcw=true)]
public class ForkJoinPool : Java.Util.Concurrent.AbstractExecutorService
[<Android.Runtime.Register("java/util/concurrent/ForkJoinPool", DoNotGenerateAcw=true)>]
type ForkJoinPool = class
inherit AbstractExecutorService
- Inheritance
- Attributes
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 <em>work-stealing</em>: 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 <em>asyncMode</em> to true in constructors, ForkJoinPool
s may also be appropriate for use with event-style tasks that are never joined. All worker threads are initialized with Thread#isDaemon
set true
.
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. The default policies may be overridden using a constructor with parameters corresponding to those documented in class ThreadPoolExecutor
.
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.
<table class="plain"> <caption>Summary of task execution methods</caption> <tr> <td></td> <th scope="col"> Call from non-fork/join clients</th> <th scope="col"> Call from within fork/join computations</th> </tr> <tr> <th scope="row" style="text-align:left"> Arrange async execution</th> <td> #execute(ForkJoinTask)
</td> <td> ForkJoinTask#fork
</td> </tr> <tr> <th scope="row" style="text-align:left"> Await and obtain result</th> <td> #invoke(ForkJoinTask)
</td> <td> ForkJoinTask#invoke
</td> </tr> <tr> <th scope="row" style="text-align:left"> Arrange exec and obtain Future</th> <td> #submit(ForkJoinTask)
</td> <td> ForkJoinTask#fork
(ForkJoinTasks <em>are</em> Futures)</td> </tr> </table>
The parameters used to construct the common pool may be controlled by setting the following System#getProperty system properties: <ul> <li>java.util.concurrent.ForkJoinPool.common.parallelism
- the parallelism level, a non-negative integer <li>java.util.concurrent.ForkJoinPool.common.threadFactory
- the class name of a ForkJoinWorkerThreadFactory
. The ClassLoader#getSystemClassLoader() system class loader is used to load this class. <li>java.util.concurrent.ForkJoinPool.common.exceptionHandler
- the class name of a UncaughtExceptionHandler
. The ClassLoader#getSystemClassLoader() system class loader is used to load this class. <li>java.util.concurrent.ForkJoinPool.common.maximumSpares
- the maximum number of allowed extra threads to maintain target parallelism (default 256). </ul> If no thread factory is supplied via a system property, then the common pool uses a factory that uses the system class loader as the Thread#getContextClassLoader() thread context class loader. In addition, if a SecurityManager
is present, then the common pool uses a factory supplying threads that have no Permissions
enabled.
Upon any error in establishing these settings, default parameters are used. It is possible to disable or limit the use of threads in the common pool by setting the parallelism property to zero, and/or using a factory that may return null
. However doing so may cause unjoined tasks to never be executed.
<b>Implementation notes:</b> 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.
Added in 1.7.
Java documentation for java.util.concurrent.ForkJoinPool
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Fork |
Creates a |
Fork |
|
Fork |
Creates a |
Fork |
Creates a |
Fork |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
Active |
Returns an estimate of the number of threads that are currently stealing or executing tasks. |
Async |
Returns |
Class |
Returns the runtime class of this |
Common |
Returns the targeted parallelism level of the common pool. |
Default |
Creates a new ForkJoinWorkerThread. |
Factory |
Returns the factory used for constructing new workers. |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
Has |
Returns |
Is |
Returns |
Is |
Returns |
Is |
Returns |
Is |
Returns |
Jni |
(Inherited from Object) |
Jni |
|
Parallelism |
Returns the targeted parallelism level of this pool. |
Peer |
(Inherited from Object) |
Pool |
Returns the number of worker threads that have started but not yet terminated. |
Queued |
Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing. |
Queued |
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). |
Running |
Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization. |
Steal |
Returns an estimate of the total number of completed tasks that were executed by a thread other than their submitter. |
Threshold |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Threshold |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Uncaught |
Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks. |
Await |
If called by a ForkJoinTask operating in this pool, equivalent
in effect to |
Await |
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. |
Await |
(Inherited from AbstractExecutorService) |
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
Common |
Returns the common pool instance. |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Drain |
Removes all available unexecuted submitted and forked tasks from scheduling queues and adds them to the given collection, without altering their execution status. |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
Execute(Fork |
Arranges for (asynchronous) execution of the given task. |
Execute(IRunnable) | |
Get |
Returns a hash code value for the object. (Inherited from Object) |
Invoke(Fork |
Performs the given task, returning its result upon completion. |
Invoke |
(Inherited from AbstractExecutorService) |
Invoke |
(Inherited from AbstractExecutorService) |
Invoke |
(Inherited from AbstractExecutorService) |
Invoke |
(Inherited from AbstractExecutorService) |
Java |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Managed |
Runs the given possibly blocking task. |
New |
Returns a |
New |
Returns a |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
Notify |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Poll |
Removes and returns the next unexecuted submission if one is available. |
Set |
Sets the Handle property. (Inherited from Object) |
Shutdown() |
Possibly initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. |
Shutdown |
Possibly attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks. |
Submit(Fork |
Submits a ForkJoinTask for execution. |
Submit(ICallable) | (Inherited from AbstractExecutorService) |
Submit(IRunnable, Object) | (Inherited from AbstractExecutorService) |
Submit(IRunnable) |
Submits a Runnable task for execution and returns a Future representing that task. (Inherited from AbstractExecutorService) |
To |
(Inherited from Object) |
To |
Returns a string representation of the object. (Inherited from Object) |
Unregister |
(Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |