Sdílet prostřednictvím


ForkJoinPool Class

Definition

An ExecutorService for running ForkJoinTasks.

[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

Remarks

An ExecutorService for running ForkJoinTasks. 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 ForkJoinTasks), 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, ForkJoinPools 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.

Constructors

ForkJoinPool()

Creates a ForkJoinPool with parallelism equal to java.lang.Runtime#availableProcessors, using defaults for all other parameters (see #ForkJoinPool(int, ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, int, int, int, Predicate, long, TimeUnit)).

ForkJoinPool(Int32, ForkJoinPool+IForkJoinWorkerThreadFactory, Thread+IUncaughtExceptionHandler, Boolean, Int32, Int32, Int32, IPredicate, Int64, TimeUnit)
ForkJoinPool(Int32, ForkJoinPool+IForkJoinWorkerThreadFactory, Thread+IUncaughtExceptionHandler, Boolean)

Creates a ForkJoinPool with parallelism equal to java.lang.Runtime#availableProcessors, using the #defaultForkJoinWorkerThreadFactory default thread factory, no UncaughtExceptionHandler, and non-async LIFO processing mode.

ForkJoinPool(Int32)

Creates a ForkJoinPool with the indicated parallelism level, using defaults for all other parameters (see #ForkJoinPool(int, ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, int, int, int, Predicate, long, TimeUnit)).

ForkJoinPool(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

ActiveThreadCount

Returns an estimate of the number of threads that are currently stealing or executing tasks.

AsyncMode

Returns true if this pool uses local first-in-first-out scheduling mode for forked tasks that are never joined.

Class

Returns the runtime class of this Object.

(Inherited from Object)
CommonPoolParallelism

Returns the targeted parallelism level of the common pool.

DefaultForkJoinWorkerThreadFactory

Creates a new ForkJoinWorkerThread.

Factory

Returns the factory used for constructing new workers.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
HasQueuedSubmissions

Returns true if there are any tasks submitted to this pool that have not yet begun executing.

IsQuiescent

Returns true if all worker threads are currently idle.

IsShutdown

Returns true if this pool has been shut down.

IsTerminated

Returns true if all tasks have completed following shut down.

IsTerminating

Returns true if the process of termination has commenced but not yet completed.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Parallelism

Returns the targeted parallelism level of this pool.

PeerReference (Inherited from Object)
PoolSize

Returns the number of worker threads that have started but not yet terminated.

QueuedSubmissionCount

Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing.

QueuedTaskCount

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).

RunningThreadCount

Returns an estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization.

StealCount

Returns an estimate of the total number of completed tasks that were executed by a thread other than their submitter.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

UncaughtExceptionHandler

Returns the handler for internal worker threads that terminate due to unrecoverable errors encountered while executing tasks.

Methods

AwaitQuiescence(Int64, TimeUnit)

If called by a ForkJoinTask operating in this pool, equivalent in effect to ForkJoinTask#helpQuiesce.

AwaitTermination(Int64, TimeUnit)

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

AwaitTerminationAsync(Int64, TimeUnit) (Inherited from AbstractExecutorService)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CommonPool()

Returns the common pool instance.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
DrainTasksTo(ICollection<ForkJoinTask>)

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(ForkJoinTask)

Arranges for (asynchronous) execution of the given task.

Execute(IRunnable)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
Invoke(ForkJoinTask)

Performs the given task, returning its result upon completion.

InvokeAll(ICollection, Int64, TimeUnit) (Inherited from AbstractExecutorService)
InvokeAll(ICollection) (Inherited from AbstractExecutorService)
InvokeAny(ICollection, Int64, TimeUnit) (Inherited from AbstractExecutorService)
InvokeAny(ICollection) (Inherited from AbstractExecutorService)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
ManagedBlock(ForkJoinPool+IManagedBlocker)

Runs the given possibly blocking task.

NewTaskFor(ICallable)

Returns a RunnableFuture for the given callable task.

(Inherited from AbstractExecutorService)
NewTaskFor(IRunnable, Object)

Returns a RunnableFuture for the given runnable and default value.

(Inherited from AbstractExecutorService)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
PollSubmission()

Removes and returns the next unexecuted submission if one is available.

SetHandle(IntPtr, JniHandleOwnership)

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.

ShutdownNow()

Possibly attempts to cancel and/or stop all tasks, and reject all subsequently submitted tasks.

Submit(ForkJoinTask)

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)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (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)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
AwaitTerminationAsync(IExecutorService, Int64, TimeUnit)
InvokeAnyAsync(IExecutorService, ICollection, Int64, TimeUnit)
InvokeAnyAsync(IExecutorService, ICollection)

Applies to