ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinPool.java
Revision: 1.79
Committed: Tue Sep 7 23:49:30 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.78: +1 -1 lines
Log Message:
typos

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 package jsr166y;
8
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.AbstractExecutorService;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.RejectedExecutionException;
20 import java.util.concurrent.RunnableFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import java.util.concurrent.locks.LockSupport;
25 import java.util.concurrent.locks.ReentrantLock;
26
27 /**
28 * An {@link ExecutorService} for running {@link ForkJoinTask}s.
29 * A {@code ForkJoinPool} provides the entry point for submissions
30 * from non-{@code ForkJoinTask} clients, as well as management and
31 * monitoring operations.
32 *
33 * <p>A {@code ForkJoinPool} differs from other kinds of {@link
34 * ExecutorService} mainly by virtue of employing
35 * <em>work-stealing</em>: all threads in the pool attempt to find and
36 * execute subtasks created by other active tasks (eventually blocking
37 * waiting for work if none exist). This enables efficient processing
38 * when most tasks spawn other subtasks (as do most {@code
39 * ForkJoinTask}s). When setting <em>asyncMode</em> to true in
40 * constructors, {@code ForkJoinPool}s may also be appropriate for use
41 * with event-style tasks that are never joined.
42 *
43 * <p>A {@code ForkJoinPool} is constructed with a given target
44 * parallelism level; by default, equal to the number of available
45 * processors. The pool attempts to maintain enough active (or
46 * available) threads by dynamically adding, suspending, or resuming
47 * internal worker threads, even if some tasks are stalled waiting to
48 * join others. However, no such adjustments are guaranteed in the
49 * face of blocked IO or other unmanaged synchronization. The nested
50 * {@link ManagedBlocker} interface enables extension of the kinds of
51 * synchronization accommodated.
52 *
53 * <p>In addition to execution and lifecycle control methods, this
54 * class provides status check methods (for example
55 * {@link #getStealCount}) that are intended to aid in developing,
56 * tuning, and monitoring fork/join applications. Also, method
57 * {@link #toString} returns indications of pool state in a
58 * convenient form for informal monitoring.
59 *
60 * <p> As is the case with other ExecutorServices, there are three
61 * main task execution methods summarized in the following
62 * table. These are designed to be used by clients not already engaged
63 * in fork/join computations in the current pool. The main forms of
64 * these methods accept instances of {@code ForkJoinTask}, but
65 * overloaded forms also allow mixed execution of plain {@code
66 * Runnable}- or {@code Callable}- based activities as well. However,
67 * tasks that are already executing in a pool should normally
68 * <em>NOT</em> use these pool execution methods, but instead use the
69 * within-computation forms listed in the table.
70 *
71 * <table BORDER CELLPADDING=3 CELLSPACING=1>
72 * <tr>
73 * <td></td>
74 * <td ALIGN=CENTER> <b>Call from non-fork/join clients</b></td>
75 * <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td>
76 * </tr>
77 * <tr>
78 * <td> <b>Arrange async execution</td>
79 * <td> {@link #execute(ForkJoinTask)}</td>
80 * <td> {@link ForkJoinTask#fork}</td>
81 * </tr>
82 * <tr>
83 * <td> <b>Await and obtain result</td>
84 * <td> {@link #invoke(ForkJoinTask)}</td>
85 * <td> {@link ForkJoinTask#invoke}</td>
86 * </tr>
87 * <tr>
88 * <td> <b>Arrange exec and obtain Future</td>
89 * <td> {@link #submit(ForkJoinTask)}</td>
90 * <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td>
91 * </tr>
92 * </table>
93 *
94 * <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is
95 * used for all parallel task execution in a program or subsystem.
96 * Otherwise, use would not usually outweigh the construction and
97 * bookkeeping overhead of creating a large set of threads. For
98 * example, a common pool could be used for the {@code SortTasks}
99 * illustrated in {@link RecursiveAction}. Because {@code
100 * ForkJoinPool} uses threads in {@linkplain java.lang.Thread#isDaemon
101 * daemon} mode, there is typically no need to explicitly {@link
102 * #shutdown} such a pool upon program exit.
103 *
104 * <pre>
105 * static final ForkJoinPool mainPool = new ForkJoinPool();
106 * ...
107 * public void sort(long[] array) {
108 * mainPool.invoke(new SortTask(array, 0, array.length));
109 * }
110 * </pre>
111 *
112 * <p><b>Implementation notes</b>: This implementation restricts the
113 * maximum number of running threads to 32767. Attempts to create
114 * pools with greater than the maximum number result in
115 * {@code IllegalArgumentException}.
116 *
117 * <p>This implementation rejects submitted tasks (that is, by throwing
118 * {@link RejectedExecutionException}) only when the pool is shut down
119 * or internal resources have been exhausted.
120 *
121 * @since 1.7
122 * @author Doug Lea
123 */
124 public class ForkJoinPool extends AbstractExecutorService {
125
126 /*
127 * Implementation Overview
128 *
129 * This class provides the central bookkeeping and control for a
130 * set of worker threads: Submissions from non-FJ threads enter
131 * into a submission queue. Workers take these tasks and typically
132 * split them into subtasks that may be stolen by other workers.
133 * The main work-stealing mechanics implemented in class
134 * ForkJoinWorkerThread give first priority to processing tasks
135 * from their own queues (LIFO or FIFO, depending on mode), then
136 * to randomized FIFO steals of tasks in other worker queues, and
137 * lastly to new submissions. These mechanics do not consider
138 * affinities, loads, cache localities, etc, so rarely provide the
139 * best possible performance on a given machine, but portably
140 * provide good throughput by averaging over these factors.
141 * (Further, even if we did try to use such information, we do not
142 * usually have a basis for exploiting it. For example, some sets
143 * of tasks profit from cache affinities, but others are harmed by
144 * cache pollution effects.)
145 *
146 * Beyond work-stealing support and essential bookkeeping, the
147 * main responsibility of this framework is to take actions when
148 * one worker is waiting to join a task stolen (or always held by)
149 * another. Because we are multiplexing many tasks on to a pool
150 * of workers, we can't just let them block (as in Thread.join).
151 * We also cannot just reassign the joiner's run-time stack with
152 * another and replace it later, which would be a form of
153 * "continuation", that even if possible is not necessarily a good
154 * idea. Given that the creation costs of most threads on most
155 * systems mainly surrounds setting up runtime stacks, thread
156 * creation and switching is usually not much more expensive than
157 * stack creation and switching, and is more flexible). Instead we
158 * combine two tactics:
159 *
160 * Helping: Arranging for the joiner to execute some task that it
161 * would be running if the steal had not occurred. Method
162 * ForkJoinWorkerThread.helpJoinTask tracks joining->stealing
163 * links to try to find such a task.
164 *
165 * Compensating: Unless there are already enough live threads,
166 * method helpMaintainParallelism() may create or
167 * re-activate a spare thread to compensate for blocked
168 * joiners until they unblock.
169 *
170 * It is impossible to keep exactly the target (parallelism)
171 * number of threads running at any given time. Determining
172 * existence of conservatively safe helping targets, the
173 * availability of already-created spares, and the apparent need
174 * to create new spares are all racy and require heuristic
175 * guidance, so we rely on multiple retries of each. Compensation
176 * occurs in slow-motion. It is triggered only upon timeouts of
177 * Object.wait used for joins. This reduces poor decisions that
178 * would otherwise be made when threads are waiting for others
179 * that are stalled because of unrelated activities such as
180 * garbage collection.
181 *
182 * The ManagedBlocker extension API can't use helping so relies
183 * only on compensation in method awaitBlocker.
184 *
185 * The main throughput advantages of work-stealing stem from
186 * decentralized control -- workers mostly steal tasks from each
187 * other. We do not want to negate this by creating bottlenecks
188 * implementing other management responsibilities. So we use a
189 * collection of techniques that avoid, reduce, or cope well with
190 * contention. These entail several instances of bit-packing into
191 * CASable fields to maintain only the minimally required
192 * atomicity. To enable such packing, we restrict maximum
193 * parallelism to (1<<15)-1 (enabling twice this (to accommodate
194 * unbalanced increments and decrements) to fit into a 16 bit
195 * field, which is far in excess of normal operating range. Even
196 * though updates to some of these bookkeeping fields do sometimes
197 * contend with each other, they don't normally cache-contend with
198 * updates to others enough to warrant memory padding or
199 * isolation. So they are all held as fields of ForkJoinPool
200 * objects. The main capabilities are as follows:
201 *
202 * 1. Creating and removing workers. Workers are recorded in the
203 * "workers" array. This is an array as opposed to some other data
204 * structure to support index-based random steals by workers.
205 * Updates to the array recording new workers and unrecording
206 * terminated ones are protected from each other by a lock
207 * (workerLock) but the array is otherwise concurrently readable,
208 * and accessed directly by workers. To simplify index-based
209 * operations, the array size is always a power of two, and all
210 * readers must tolerate null slots. Currently, all worker thread
211 * creation is on-demand, triggered by task submissions,
212 * replacement of terminated workers, and/or compensation for
213 * blocked workers. However, all other support code is set up to
214 * work with other policies.
215 *
216 * To ensure that we do not hold on to worker references that
217 * would prevent GC, ALL accesses to workers are via indices into
218 * the workers array (which is one source of some of the unusual
219 * code constructions here). In essence, the workers array serves
220 * as a WeakReference mechanism. Thus for example the event queue
221 * stores worker indices, not worker references. Access to the
222 * workers in associated methods (for example releaseEventWaiters)
223 * must both index-check and null-check the IDs. All such accesses
224 * ignore bad IDs by returning out early from what they are doing,
225 * since this can only be associated with shutdown, in which case
226 * it is OK to give up. On termination, we just clobber these
227 * data structures without trying to use them.
228 *
229 * 2. Bookkeeping for dynamically adding and removing workers. We
230 * aim to approximately maintain the given level of parallelism.
231 * When some workers are known to be blocked (on joins or via
232 * ManagedBlocker), we may create or resume others to take their
233 * place until they unblock (see below). Implementing this
234 * requires counts of the number of "running" threads (i.e., those
235 * that are neither blocked nor artificially suspended) as well as
236 * the total number. These two values are packed into one field,
237 * "workerCounts" because we need accurate snapshots when deciding
238 * to create, resume or suspend. Note however that the
239 * correspondence of these counts to reality is not guaranteed. In
240 * particular updates for unblocked threads may lag until they
241 * actually wake up.
242 *
243 * 3. Maintaining global run state. The run state of the pool
244 * consists of a runLevel (SHUTDOWN, TERMINATING, etc) similar to
245 * those in other Executor implementations, as well as a count of
246 * "active" workers -- those that are, or soon will be, or
247 * recently were executing tasks. The runLevel and active count
248 * are packed together in order to correctly trigger shutdown and
249 * termination. Without care, active counts can be subject to very
250 * high contention. We substantially reduce this contention by
251 * relaxing update rules. A worker must claim active status
252 * prospectively, by activating if it sees that a submitted or
253 * stealable task exists (it may find after activating that the
254 * task no longer exists). It stays active while processing this
255 * task (if it exists) and any other local subtasks it produces,
256 * until it cannot find any other tasks. It then tries
257 * inactivating (see method preStep), but upon update contention
258 * instead scans for more tasks, later retrying inactivation if it
259 * doesn't find any.
260 *
261 * 4. Managing idle workers waiting for tasks. We cannot let
262 * workers spin indefinitely scanning for tasks when none are
263 * available. On the other hand, we must quickly prod them into
264 * action when new tasks are submitted or generated. We
265 * park/unpark these idle workers using an event-count scheme.
266 * Field eventCount is incremented upon events that may enable
267 * workers that previously could not find a task to now find one:
268 * Submission of a new task to the pool, or another worker pushing
269 * a task onto a previously empty queue. (We also use this
270 * mechanism for configuration and termination actions that
271 * require wakeups of idle workers). Each worker maintains its
272 * last known event count, and blocks when a scan for work did not
273 * find a task AND its lastEventCount matches the current
274 * eventCount. Waiting idle workers are recorded in a variant of
275 * Treiber stack headed by field eventWaiters which, when nonzero,
276 * encodes the thread index and count awaited for by the worker
277 * thread most recently calling eventSync. This thread in turn has
278 * a record (field nextEventWaiter) for the next waiting worker.
279 * In addition to allowing simpler decisions about need for
280 * wakeup, the event count bits in eventWaiters serve the role of
281 * tags to avoid ABA errors in Treiber stacks. Upon any wakeup,
282 * released threads also try to release at most two others. The
283 * net effect is a tree-like diffusion of signals, where released
284 * threads (and possibly others) help with unparks. To further
285 * reduce contention effects a bit, failed CASes to increment
286 * field eventCount are tolerated without retries in signalWork.
287 * Conceptually they are merged into the same event, which is OK
288 * when their only purpose is to enable workers to scan for work.
289 *
290 * 5. Managing suspension of extra workers. When a worker notices
291 * (usually upon timeout of a wait()) that there are too few
292 * running threads, we may create a new thread to maintain
293 * parallelism level, or at least avoid starvation. Usually, extra
294 * threads are needed for only very short periods, yet join
295 * dependencies are such that we sometimes need them in
296 * bursts. Rather than create new threads each time this happens,
297 * we suspend no-longer-needed extra ones as "spares". For most
298 * purposes, we don't distinguish "extra" spare threads from
299 * normal "core" threads: On each call to preStep (the only point
300 * at which we can do this) a worker checks to see if there are
301 * now too many running workers, and if so, suspends itself.
302 * Method helpMaintainParallelism looks for suspended threads to
303 * resume before considering creating a new replacement. The
304 * spares themselves are encoded on another variant of a Treiber
305 * Stack, headed at field "spareWaiters". Note that the use of
306 * spares is intrinsically racy. One thread may become a spare at
307 * about the same time as another is needlessly being created. We
308 * counteract this and related slop in part by requiring resumed
309 * spares to immediately recheck (in preStep) to see whether they
310 * should re-suspend.
311 *
312 * 6. Killing off unneeded workers. A timeout mechanism is used to
313 * shed unused workers: The oldest (first) event queue waiter uses
314 * a timed rather than hard wait. When this wait times out without
315 * a normal wakeup, it tries to shutdown any one (for convenience
316 * the newest) other spare or event waiter via
317 * tryShutdownUnusedWorker. This eventually reduces the number of
318 * worker threads to a minimum of one after a long enough period
319 * without use.
320 *
321 * 7. Deciding when to create new workers. The main dynamic
322 * control in this class is deciding when to create extra threads
323 * in method helpMaintainParallelism. We would like to keep
324 * exactly #parallelism threads running, which is an impossible
325 * task. We always need to create one when the number of running
326 * threads would become zero and all workers are busy. Beyond
327 * this, we must rely on heuristics that work well in the
328 * presence of transient phenomena such as GC stalls, dynamic
329 * compilation, and wake-up lags. These transients are extremely
330 * common -- we are normally trying to fully saturate the CPUs on
331 * a machine, so almost any activity other than running tasks
332 * impedes accuracy. Our main defense is to allow parallelism to
333 * lapse for a while during joins, and use a timeout to see if,
334 * after the resulting settling, there is still a need for
335 * additional workers. This also better copes with the fact that
336 * some of the methods in this class tend to never become compiled
337 * (but are interpreted), so some components of the entire set of
338 * controls might execute 100 times faster than others. And
339 * similarly for cases where the apparent lack of work is just due
340 * to GC stalls and other transient system activity.
341 *
342 * Beware that there is a lot of representation-level coupling
343 * among classes ForkJoinPool, ForkJoinWorkerThread, and
344 * ForkJoinTask. For example, direct access to "workers" array by
345 * workers, and direct access to ForkJoinTask.status by both
346 * ForkJoinPool and ForkJoinWorkerThread. There is little point
347 * trying to reduce this, since any associated future changes in
348 * representations will need to be accompanied by algorithmic
349 * changes anyway.
350 *
351 * Style notes: There are lots of inline assignments (of form
352 * "while ((local = field) != 0)") which are usually the simplest
353 * way to ensure the required read orderings (which are sometimes
354 * critical). Also several occurrences of the unusual "do {}
355 * while (!cas...)" which is the simplest way to force an update of
356 * a CAS'ed variable. There are also other coding oddities that
357 * help some methods perform reasonably even when interpreted (not
358 * compiled), at the expense of some messy constructions that
359 * reduce byte code counts.
360 *
361 * The order of declarations in this file is: (1) statics (2)
362 * fields (along with constants used when unpacking some of them)
363 * (3) internal control methods (4) callbacks and other support
364 * for ForkJoinTask and ForkJoinWorkerThread classes, (5) exported
365 * methods (plus a few little helpers).
366 */
367
368 /**
369 * Factory for creating new {@link ForkJoinWorkerThread}s.
370 * A {@code ForkJoinWorkerThreadFactory} must be defined and used
371 * for {@code ForkJoinWorkerThread} subclasses that extend base
372 * functionality or initialize threads with different contexts.
373 */
374 public static interface ForkJoinWorkerThreadFactory {
375 /**
376 * Returns a new worker thread operating in the given pool.
377 *
378 * @param pool the pool this thread works in
379 * @throws NullPointerException if the pool is null
380 */
381 public ForkJoinWorkerThread newThread(ForkJoinPool pool);
382 }
383
384 /**
385 * Default ForkJoinWorkerThreadFactory implementation; creates a
386 * new ForkJoinWorkerThread.
387 */
388 static class DefaultForkJoinWorkerThreadFactory
389 implements ForkJoinWorkerThreadFactory {
390 public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
391 return new ForkJoinWorkerThread(pool);
392 }
393 }
394
395 /**
396 * Creates a new ForkJoinWorkerThread. This factory is used unless
397 * overridden in ForkJoinPool constructors.
398 */
399 public static final ForkJoinWorkerThreadFactory
400 defaultForkJoinWorkerThreadFactory =
401 new DefaultForkJoinWorkerThreadFactory();
402
403 /**
404 * Permission required for callers of methods that may start or
405 * kill threads.
406 */
407 private static final RuntimePermission modifyThreadPermission =
408 new RuntimePermission("modifyThread");
409
410 /**
411 * If there is a security manager, makes sure caller has
412 * permission to modify threads.
413 */
414 private static void checkPermission() {
415 SecurityManager security = System.getSecurityManager();
416 if (security != null)
417 security.checkPermission(modifyThreadPermission);
418 }
419
420 /**
421 * Generator for assigning sequence numbers as pool names.
422 */
423 private static final AtomicInteger poolNumberGenerator =
424 new AtomicInteger();
425
426 /**
427 * The time to block in a join (see awaitJoin) before checking if
428 * a new worker should be (re)started to maintain parallelism
429 * level. The value should be short enough to maintain global
430 * responsiveness and progress but long enough to avoid
431 * counterproductive firings during GC stalls or unrelated system
432 * activity, and to not bog down systems with continual re-firings
433 * on GCs or legitimately long waits.
434 */
435 private static final long JOIN_TIMEOUT_MILLIS = 250L; // 4 per second
436
437 /**
438 * The wakeup interval (in nanoseconds) for the oldest worker
439 * waiting for an event to invoke tryShutdownUnusedWorker to
440 * shrink the number of workers. The exact value does not matter
441 * too much. It must be short enough to release resources during
442 * sustained periods of idleness, but not so short that threads
443 * are continually re-created.
444 */
445 private static final long SHRINK_RATE_NANOS =
446 30L * 1000L * 1000L * 1000L; // 2 per minute
447
448 /**
449 * Absolute bound for parallelism level. Twice this number plus
450 * one (i.e., 0xfff) must fit into a 16bit field to enable
451 * word-packing for some counts and indices.
452 */
453 private static final int MAX_WORKERS = 0x7fff;
454
455 /**
456 * Array holding all worker threads in the pool. Array size must
457 * be a power of two. Updates and replacements are protected by
458 * workerLock, but the array is always kept in a consistent enough
459 * state to be randomly accessed without locking by workers
460 * performing work-stealing, as well as other traversal-based
461 * methods in this class. All readers must tolerate that some
462 * array slots may be null.
463 */
464 volatile ForkJoinWorkerThread[] workers;
465
466 /**
467 * Queue for external submissions.
468 */
469 private final LinkedTransferQueue<ForkJoinTask<?>> submissionQueue;
470
471 /**
472 * Lock protecting updates to workers array.
473 */
474 private final ReentrantLock workerLock;
475
476 /**
477 * Latch released upon termination.
478 */
479 private final Phaser termination;
480
481 /**
482 * Creation factory for worker threads.
483 */
484 private final ForkJoinWorkerThreadFactory factory;
485
486 /**
487 * Sum of per-thread steal counts, updated only when threads are
488 * idle or terminating.
489 */
490 private volatile long stealCount;
491
492 /**
493 * Encoded record of top of Treiber stack of threads waiting for
494 * events. The top 32 bits contain the count being waited for. The
495 * bottom 16 bits contains one plus the pool index of waiting
496 * worker thread. (Bits 16-31 are unused.)
497 */
498 private volatile long eventWaiters;
499
500 private static final int EVENT_COUNT_SHIFT = 32;
501 private static final long WAITER_ID_MASK = (1L << 16) - 1L;
502
503 /**
504 * A counter for events that may wake up worker threads:
505 * - Submission of a new task to the pool
506 * - A worker pushing a task on an empty queue
507 * - termination
508 */
509 private volatile int eventCount;
510
511 /**
512 * Encoded record of top of Treiber stack of spare threads waiting
513 * for resumption. The top 16 bits contain an arbitrary count to
514 * avoid ABA effects. The bottom 16bits contains one plus the pool
515 * index of waiting worker thread.
516 */
517 private volatile int spareWaiters;
518
519 private static final int SPARE_COUNT_SHIFT = 16;
520 private static final int SPARE_ID_MASK = (1 << 16) - 1;
521
522 /**
523 * Lifecycle control. The low word contains the number of workers
524 * that are (probably) executing tasks. This value is atomically
525 * incremented before a worker gets a task to run, and decremented
526 * when a worker has no tasks and cannot find any. Bits 16-18
527 * contain runLevel value. When all are zero, the pool is
528 * running. Level transitions are monotonic (running -> shutdown
529 * -> terminating -> terminated) so each transition adds a bit.
530 * These are bundled together to ensure consistent read for
531 * termination checks (i.e., that runLevel is at least SHUTDOWN
532 * and active threads is zero).
533 *
534 * Notes: Most direct CASes are dependent on these bitfield
535 * positions. Also, this field is non-private to enable direct
536 * performance-sensitive CASes in ForkJoinWorkerThread.
537 */
538 volatile int runState;
539
540 // Note: The order among run level values matters.
541 private static final int RUNLEVEL_SHIFT = 16;
542 private static final int SHUTDOWN = 1 << RUNLEVEL_SHIFT;
543 private static final int TERMINATING = 1 << (RUNLEVEL_SHIFT + 1);
544 private static final int TERMINATED = 1 << (RUNLEVEL_SHIFT + 2);
545 private static final int ACTIVE_COUNT_MASK = (1 << RUNLEVEL_SHIFT) - 1;
546
547 /**
548 * Holds number of total (i.e., created and not yet terminated)
549 * and running (i.e., not blocked on joins or other managed sync)
550 * threads, packed together to ensure consistent snapshot when
551 * making decisions about creating and suspending spare
552 * threads. Updated only by CAS. Note that adding a new worker
553 * requires incrementing both counts, since workers start off in
554 * running state.
555 */
556 private volatile int workerCounts;
557
558 private static final int TOTAL_COUNT_SHIFT = 16;
559 private static final int RUNNING_COUNT_MASK = (1 << TOTAL_COUNT_SHIFT) - 1;
560 private static final int ONE_RUNNING = 1;
561 private static final int ONE_TOTAL = 1 << TOTAL_COUNT_SHIFT;
562
563 /**
564 * The target parallelism level.
565 * Accessed directly by ForkJoinWorkerThreads.
566 */
567 final int parallelism;
568
569 /**
570 * True if use local fifo, not default lifo, for local polling
571 * Read by, and replicated by ForkJoinWorkerThreads
572 */
573 final boolean locallyFifo;
574
575 /**
576 * The uncaught exception handler used when any worker abruptly
577 * terminates.
578 */
579 private final Thread.UncaughtExceptionHandler ueh;
580
581 /**
582 * Pool number, just for assigning useful names to worker threads
583 */
584 private final int poolNumber;
585
586 // Utilities for CASing fields. Note that most of these
587 // are usually manually inlined by callers
588
589 /**
590 * Increments running count part of workerCounts
591 */
592 final void incrementRunningCount() {
593 int c;
594 do {} while (!UNSAFE.compareAndSwapInt(this, workerCountsOffset,
595 c = workerCounts,
596 c + ONE_RUNNING));
597 }
598
599 /**
600 * Tries to decrement running count unless already zero
601 */
602 final boolean tryDecrementRunningCount() {
603 int wc = workerCounts;
604 if ((wc & RUNNING_COUNT_MASK) == 0)
605 return false;
606 return UNSAFE.compareAndSwapInt(this, workerCountsOffset,
607 wc, wc - ONE_RUNNING);
608 }
609
610 /**
611 * Forces decrement of encoded workerCounts, awaiting nonzero if
612 * (rarely) necessary when other count updates lag.
613 *
614 * @param dr -- either zero or ONE_RUNNING
615 * @param dt -- either zero or ONE_TOTAL
616 */
617 private void decrementWorkerCounts(int dr, int dt) {
618 for (;;) {
619 int wc = workerCounts;
620 if ((wc & RUNNING_COUNT_MASK) - dr < 0 ||
621 (wc >>> TOTAL_COUNT_SHIFT) - dt < 0) {
622 if ((runState & TERMINATED) != 0)
623 return; // lagging termination on a backout
624 Thread.yield();
625 }
626 if (UNSAFE.compareAndSwapInt(this, workerCountsOffset,
627 wc, wc - (dr + dt)))
628 return;
629 }
630 }
631
632 /**
633 * Tries decrementing active count; fails on contention.
634 * Called when workers cannot find tasks to run.
635 */
636 final boolean tryDecrementActiveCount() {
637 int c;
638 return UNSAFE.compareAndSwapInt(this, runStateOffset,
639 c = runState, c - 1);
640 }
641
642 /**
643 * Advances to at least the given level. Returns true if not
644 * already in at least the given level.
645 */
646 private boolean advanceRunLevel(int level) {
647 for (;;) {
648 int s = runState;
649 if ((s & level) != 0)
650 return false;
651 if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, s | level))
652 return true;
653 }
654 }
655
656 // workers array maintenance
657
658 /**
659 * Records and returns a workers array index for new worker.
660 */
661 private int recordWorker(ForkJoinWorkerThread w) {
662 // Try using slot totalCount-1. If not available, scan and/or resize
663 int k = (workerCounts >>> TOTAL_COUNT_SHIFT) - 1;
664 final ReentrantLock lock = this.workerLock;
665 lock.lock();
666 try {
667 ForkJoinWorkerThread[] ws = workers;
668 int n = ws.length;
669 if (k < 0 || k >= n || ws[k] != null) {
670 for (k = 0; k < n && ws[k] != null; ++k)
671 ;
672 if (k == n)
673 ws = Arrays.copyOf(ws, n << 1);
674 }
675 ws[k] = w;
676 workers = ws; // volatile array write ensures slot visibility
677 } finally {
678 lock.unlock();
679 }
680 return k;
681 }
682
683 /**
684 * Nulls out record of worker in workers array.
685 */
686 private void forgetWorker(ForkJoinWorkerThread w) {
687 int idx = w.poolIndex;
688 // Locking helps method recordWorker avoid unnecessary expansion
689 final ReentrantLock lock = this.workerLock;
690 lock.lock();
691 try {
692 ForkJoinWorkerThread[] ws = workers;
693 if (idx >= 0 && idx < ws.length && ws[idx] == w) // verify
694 ws[idx] = null;
695 } finally {
696 lock.unlock();
697 }
698 }
699
700 /**
701 * Final callback from terminating worker. Removes record of
702 * worker from array, and adjusts counts. If pool is shutting
703 * down, tries to complete termination.
704 *
705 * @param w the worker
706 */
707 final void workerTerminated(ForkJoinWorkerThread w) {
708 forgetWorker(w);
709 decrementWorkerCounts(w.isTrimmed()? 0 : ONE_RUNNING, ONE_TOTAL);
710 while (w.stealCount != 0) // collect final count
711 tryAccumulateStealCount(w);
712 tryTerminate(false);
713 }
714
715 // Waiting for and signalling events
716
717 /**
718 * Releases workers blocked on a count not equal to current count.
719 * Normally called after precheck that eventWaiters isn't zero to
720 * avoid wasted array checks. Gives up upon a change in count or
721 * upon releasing two workers, letting others take over.
722 */
723 private void releaseEventWaiters() {
724 ForkJoinWorkerThread[] ws = workers;
725 int n = ws.length;
726 long h = eventWaiters;
727 int ec = eventCount;
728 boolean releasedOne = false;
729 ForkJoinWorkerThread w; int id;
730 while ((id = ((int)(h & WAITER_ID_MASK)) - 1) >= 0 &&
731 (int)(h >>> EVENT_COUNT_SHIFT) != ec &&
732 id < n && (w = ws[id]) != null) {
733 if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
734 h, w.nextWaiter)) {
735 LockSupport.unpark(w);
736 if (releasedOne) // exit on second release
737 break;
738 releasedOne = true;
739 }
740 if (eventCount != ec)
741 break;
742 h = eventWaiters;
743 }
744 }
745
746 /**
747 * Tries to advance eventCount and releases waiters. Called only
748 * from workers.
749 */
750 final void signalWork() {
751 int c; // try to increment event count -- CAS failure OK
752 UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
753 if (eventWaiters != 0L)
754 releaseEventWaiters();
755 }
756
757 /**
758 * Adds the given worker to event queue and blocks until
759 * terminating or event count advances from the given value
760 *
761 * @param w the calling worker thread
762 * @param ec the count
763 */
764 private void eventSync(ForkJoinWorkerThread w, int ec) {
765 long nh = (((long)ec) << EVENT_COUNT_SHIFT) | ((long)(w.poolIndex+1));
766 long h;
767 while ((runState < SHUTDOWN || !tryTerminate(false)) &&
768 (((int)((h = eventWaiters) & WAITER_ID_MASK)) == 0 ||
769 (int)(h >>> EVENT_COUNT_SHIFT) == ec) &&
770 eventCount == ec) {
771 if (UNSAFE.compareAndSwapLong(this, eventWaitersOffset,
772 w.nextWaiter = h, nh)) {
773 awaitEvent(w, ec);
774 break;
775 }
776 }
777 }
778
779 /**
780 * Blocks the given worker (that has already been entered as an
781 * event waiter) until terminating or event count advances from
782 * the given value. The oldest (first) waiter uses a timed wait to
783 * occasionally one-by-one shrink the number of workers (to a
784 * minimum of one) if the pool has not been used for extended
785 * periods.
786 *
787 * @param w the calling worker thread
788 * @param ec the count
789 */
790 private void awaitEvent(ForkJoinWorkerThread w, int ec) {
791 while (eventCount == ec) {
792 if (tryAccumulateStealCount(w)) { // transfer while idle
793 boolean untimed = (w.nextWaiter != 0L ||
794 (workerCounts & RUNNING_COUNT_MASK) <= 1);
795 long startTime = untimed? 0 : System.nanoTime();
796 Thread.interrupted(); // clear/ignore interrupt
797 if (eventCount != ec || w.runState != 0 ||
798 runState >= TERMINATING) // recheck after clear
799 break;
800 if (untimed)
801 LockSupport.park(w);
802 else {
803 LockSupport.parkNanos(w, SHRINK_RATE_NANOS);
804 if (eventCount != ec || w.runState != 0 ||
805 runState >= TERMINATING)
806 break;
807 if (System.nanoTime() - startTime >= SHRINK_RATE_NANOS)
808 tryShutdownUnusedWorker(ec);
809 }
810 }
811 }
812 }
813
814 // Maintaining parallelism
815
816 /**
817 * Pushes worker onto the spare stack.
818 */
819 final void pushSpare(ForkJoinWorkerThread w) {
820 int ns = (++w.spareCount << SPARE_COUNT_SHIFT) | (w.poolIndex + 1);
821 do {} while (!UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
822 w.nextSpare = spareWaiters,ns));
823 }
824
825 /**
826 * Tries (once) to resume a spare if the number of running
827 * threads is less than target.
828 */
829 private void tryResumeSpare() {
830 int sw, id;
831 ForkJoinWorkerThread[] ws = workers;
832 int n = ws.length;
833 ForkJoinWorkerThread w;
834 if ((sw = spareWaiters) != 0 &&
835 (id = (sw & SPARE_ID_MASK) - 1) >= 0 &&
836 id < n && (w = ws[id]) != null &&
837 (workerCounts & RUNNING_COUNT_MASK) < parallelism &&
838 spareWaiters == sw &&
839 UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
840 sw, w.nextSpare)) {
841 int c; // increment running count before resume
842 do {} while (!UNSAFE.compareAndSwapInt
843 (this, workerCountsOffset,
844 c = workerCounts, c + ONE_RUNNING));
845 if (w.tryUnsuspend())
846 LockSupport.unpark(w);
847 else // back out if w was shutdown
848 decrementWorkerCounts(ONE_RUNNING, 0);
849 }
850 }
851
852 /**
853 * Tries to increase the number of running workers if below target
854 * parallelism: If a spare exists tries to resume it via
855 * tryResumeSpare. Otherwise, if not enough total workers or all
856 * existing workers are busy, adds a new worker. In all cases also
857 * helps wake up releasable workers waiting for work.
858 */
859 private void helpMaintainParallelism() {
860 int pc = parallelism;
861 int wc, rs, tc;
862 while (((wc = workerCounts) & RUNNING_COUNT_MASK) < pc &&
863 (rs = runState) < TERMINATING) {
864 if (spareWaiters != 0)
865 tryResumeSpare();
866 else if ((tc = wc >>> TOTAL_COUNT_SHIFT) >= MAX_WORKERS ||
867 (tc >= pc && (rs & ACTIVE_COUNT_MASK) != tc))
868 break; // enough total
869 else if (runState == rs && workerCounts == wc &&
870 UNSAFE.compareAndSwapInt(this, workerCountsOffset, wc,
871 wc + (ONE_RUNNING|ONE_TOTAL))) {
872 ForkJoinWorkerThread w = null;
873 try {
874 w = factory.newThread(this);
875 } finally { // adjust on null or exceptional factory return
876 if (w == null) {
877 decrementWorkerCounts(ONE_RUNNING, ONE_TOTAL);
878 tryTerminate(false); // handle failure during shutdown
879 }
880 }
881 if (w == null)
882 break;
883 w.start(recordWorker(w), ueh);
884 if ((workerCounts >>> TOTAL_COUNT_SHIFT) >= pc) {
885 int c; // advance event count
886 UNSAFE.compareAndSwapInt(this, eventCountOffset,
887 c = eventCount, c+1);
888 break; // add at most one unless total below target
889 }
890 }
891 }
892 if (eventWaiters != 0L)
893 releaseEventWaiters();
894 }
895
896 /**
897 * Callback from the oldest waiter in awaitEvent waking up after a
898 * period of non-use. If all workers are idle, tries (once) to
899 * shutdown an event waiter or a spare, if one exists. Note that
900 * we don't need CAS or locks here because the method is called
901 * only from one thread occasionally waking (and even misfires are
902 * OK). Note that until the shutdown worker fully terminates,
903 * workerCounts will overestimate total count, which is tolerable.
904 *
905 * @param ec the event count waited on by caller (to abort
906 * attempt if count has since changed).
907 */
908 private void tryShutdownUnusedWorker(int ec) {
909 if (runState == 0 && eventCount == ec) { // only trigger if all idle
910 ForkJoinWorkerThread[] ws = workers;
911 int n = ws.length;
912 ForkJoinWorkerThread w = null;
913 boolean shutdown = false;
914 int sw;
915 long h;
916 if ((sw = spareWaiters) != 0) { // prefer killing spares
917 int id = (sw & SPARE_ID_MASK) - 1;
918 if (id >= 0 && id < n && (w = ws[id]) != null &&
919 UNSAFE.compareAndSwapInt(this, spareWaitersOffset,
920 sw, w.nextSpare))
921 shutdown = true;
922 }
923 else if ((h = eventWaiters) != 0L) {
924 long nh;
925 int id = ((int)(h & WAITER_ID_MASK)) - 1;
926 if (id >= 0 && id < n && (w = ws[id]) != null &&
927 (nh = w.nextWaiter) != 0L && // keep at least one worker
928 UNSAFE.compareAndSwapLong(this, eventWaitersOffset, h, nh))
929 shutdown = true;
930 }
931 if (w != null && shutdown) {
932 w.shutdown();
933 LockSupport.unpark(w);
934 }
935 }
936 releaseEventWaiters(); // in case of interference
937 }
938
939 /**
940 * Callback from workers invoked upon each top-level action (i.e.,
941 * stealing a task or taking a submission and running it).
942 * Performs one or more of the following:
943 *
944 * 1. If the worker is active and either did not run a task
945 * or there are too many workers, try to set its active status
946 * to inactive and update activeCount. On contention, we may
947 * try again in this or a subsequent call.
948 *
949 * 2. If not enough total workers, help create some.
950 *
951 * 3. If there are too many running workers, suspend this worker
952 * (first forcing inactive if necessary). If it is not needed,
953 * it may be shutdown while suspended (via
954 * tryShutdownUnusedWorker). Otherwise, upon resume it
955 * rechecks running thread count and need for event sync.
956 *
957 * 4. If worker did not run a task, await the next task event via
958 * eventSync if necessary (first forcing inactivation), upon
959 * which the worker may be shutdown via
960 * tryShutdownUnusedWorker. Otherwise, help release any
961 * existing event waiters that are now releasable,
962 *
963 * @param w the worker
964 * @param ran true if worker ran a task since last call to this method
965 */
966 final void preStep(ForkJoinWorkerThread w, boolean ran) {
967 int wec = w.lastEventCount;
968 boolean active = w.active;
969 boolean inactivate = false;
970 int pc = parallelism;
971 int rs;
972 while (w.runState == 0 && (rs = runState) < TERMINATING) {
973 if ((inactivate || (active && (rs & ACTIVE_COUNT_MASK) >= pc)) &&
974 UNSAFE.compareAndSwapInt(this, runStateOffset, rs, rs - 1))
975 inactivate = active = w.active = false;
976 int wc = workerCounts;
977 if ((wc & RUNNING_COUNT_MASK) > pc) {
978 if (!(inactivate |= active) && // must inactivate to suspend
979 workerCounts == wc && // try to suspend as spare
980 UNSAFE.compareAndSwapInt(this, workerCountsOffset,
981 wc, wc - ONE_RUNNING))
982 w.suspendAsSpare();
983 }
984 else if ((wc >>> TOTAL_COUNT_SHIFT) < pc)
985 helpMaintainParallelism(); // not enough workers
986 else if (!ran) {
987 long h = eventWaiters;
988 int ec = eventCount;
989 if (h != 0L && (int)(h >>> EVENT_COUNT_SHIFT) != ec)
990 releaseEventWaiters(); // release others before waiting
991 else if (ec != wec) {
992 w.lastEventCount = ec; // no need to wait
993 break;
994 }
995 else if (!(inactivate |= active))
996 eventSync(w, wec); // must inactivate before sync
997 }
998 else
999 break;
1000 }
1001 }
1002
1003 /**
1004 * Helps and/or blocks awaiting join of the given task.
1005 * See above for explanation.
1006 *
1007 * @param joinMe the task to join
1008 * @param worker the current worker thread
1009 */
1010 final void awaitJoin(ForkJoinTask<?> joinMe, ForkJoinWorkerThread worker) {
1011 int retries = 2 + (parallelism >> 2); // #helpJoins before blocking
1012 while (joinMe.status >= 0) {
1013 int wc;
1014 worker.helpJoinTask(joinMe);
1015 if (joinMe.status < 0)
1016 break;
1017 else if (retries > 0)
1018 --retries;
1019 else if (((wc = workerCounts) & RUNNING_COUNT_MASK) != 0 &&
1020 UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1021 wc, wc - ONE_RUNNING)) {
1022 int stat, c; long h;
1023 while ((stat = joinMe.status) >= 0 &&
1024 (h = eventWaiters) != 0L && // help release others
1025 (int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
1026 releaseEventWaiters();
1027 if (stat >= 0 &&
1028 ((workerCounts & RUNNING_COUNT_MASK) == 0 ||
1029 (stat =
1030 joinMe.internalAwaitDone(JOIN_TIMEOUT_MILLIS)) >= 0))
1031 helpMaintainParallelism(); // timeout or no running workers
1032 do {} while (!UNSAFE.compareAndSwapInt
1033 (this, workerCountsOffset,
1034 c = workerCounts, c + ONE_RUNNING));
1035 if (stat < 0)
1036 break; // else restart
1037 }
1038 }
1039 }
1040
1041 /**
1042 * Same idea as awaitJoin, but no helping, retries, or timeouts.
1043 */
1044 final void awaitBlocker(ManagedBlocker blocker)
1045 throws InterruptedException {
1046 while (!blocker.isReleasable()) {
1047 int wc = workerCounts;
1048 if ((wc & RUNNING_COUNT_MASK) != 0 &&
1049 UNSAFE.compareAndSwapInt(this, workerCountsOffset,
1050 wc, wc - ONE_RUNNING)) {
1051 try {
1052 while (!blocker.isReleasable()) {
1053 long h = eventWaiters;
1054 if (h != 0L &&
1055 (int)(h >>> EVENT_COUNT_SHIFT) != eventCount)
1056 releaseEventWaiters();
1057 else if ((workerCounts & RUNNING_COUNT_MASK) == 0 &&
1058 runState < TERMINATING)
1059 helpMaintainParallelism();
1060 else if (blocker.block())
1061 break;
1062 }
1063 } finally {
1064 int c;
1065 do {} while (!UNSAFE.compareAndSwapInt
1066 (this, workerCountsOffset,
1067 c = workerCounts, c + ONE_RUNNING));
1068 }
1069 break;
1070 }
1071 }
1072 }
1073
1074 /**
1075 * Possibly initiates and/or completes termination.
1076 *
1077 * @param now if true, unconditionally terminate, else only
1078 * if shutdown and empty queue and no active workers
1079 * @return true if now terminating or terminated
1080 */
1081 private boolean tryTerminate(boolean now) {
1082 if (now)
1083 advanceRunLevel(SHUTDOWN); // ensure at least SHUTDOWN
1084 else if (runState < SHUTDOWN ||
1085 !submissionQueue.isEmpty() ||
1086 (runState & ACTIVE_COUNT_MASK) != 0)
1087 return false;
1088
1089 if (advanceRunLevel(TERMINATING))
1090 startTerminating();
1091
1092 // Finish now if all threads terminated; else in some subsequent call
1093 if ((workerCounts >>> TOTAL_COUNT_SHIFT) == 0) {
1094 advanceRunLevel(TERMINATED);
1095 termination.arrive();
1096 }
1097 return true;
1098 }
1099
1100 /**
1101 * Actions on transition to TERMINATING
1102 *
1103 * Runs up to four passes through workers: (0) shutting down each
1104 * (without waking up if parked) to quickly spread notifications
1105 * without unnecessary bouncing around event queues etc (1) wake
1106 * up and help cancel tasks (2) interrupt (3) mop up races with
1107 * interrupted workers
1108 */
1109 private void startTerminating() {
1110 cancelSubmissions();
1111 for (int passes = 0; passes < 4 && workerCounts != 0; ++passes) {
1112 int c; // advance event count
1113 UNSAFE.compareAndSwapInt(this, eventCountOffset,
1114 c = eventCount, c+1);
1115 eventWaiters = 0L; // clobber lists
1116 spareWaiters = 0;
1117 for (ForkJoinWorkerThread w : workers) {
1118 if (w != null) {
1119 w.shutdown();
1120 if (passes > 0 && !w.isTerminated()) {
1121 w.cancelTasks();
1122 LockSupport.unpark(w);
1123 if (passes > 1) {
1124 try {
1125 w.interrupt();
1126 } catch (SecurityException ignore) {
1127 }
1128 }
1129 }
1130 }
1131 }
1132 }
1133 }
1134
1135 /**
1136 * Clears out and cancels submissions, ignoring exceptions.
1137 */
1138 private void cancelSubmissions() {
1139 ForkJoinTask<?> task;
1140 while ((task = submissionQueue.poll()) != null) {
1141 try {
1142 task.cancel(false);
1143 } catch (Throwable ignore) {
1144 }
1145 }
1146 }
1147
1148 // misc support for ForkJoinWorkerThread
1149
1150 /**
1151 * Returns pool number.
1152 */
1153 final int getPoolNumber() {
1154 return poolNumber;
1155 }
1156
1157 /**
1158 * Tries to accumulate steal count from a worker, clearing
1159 * the worker's value if successful.
1160 *
1161 * @return true if worker steal count now zero
1162 */
1163 final boolean tryAccumulateStealCount(ForkJoinWorkerThread w) {
1164 int sc = w.stealCount;
1165 long c = stealCount;
1166 // CAS even if zero, for fence effects
1167 if (UNSAFE.compareAndSwapLong(this, stealCountOffset, c, c + sc)) {
1168 if (sc != 0)
1169 w.stealCount = 0;
1170 return true;
1171 }
1172 return sc == 0;
1173 }
1174
1175 /**
1176 * Returns the approximate (non-atomic) number of idle threads per
1177 * active thread.
1178 */
1179 final int idlePerActive() {
1180 int pc = parallelism; // use parallelism, not rc
1181 int ac = runState; // no mask -- artificially boosts during shutdown
1182 // Use exact results for small values, saturate past 4
1183 return ((pc <= ac) ? 0 :
1184 (pc >>> 1 <= ac) ? 1 :
1185 (pc >>> 2 <= ac) ? 3 :
1186 pc >>> 3);
1187 }
1188
1189 // Public and protected methods
1190
1191 // Constructors
1192
1193 /**
1194 * Creates a {@code ForkJoinPool} with parallelism equal to {@link
1195 * java.lang.Runtime#availableProcessors}, using the {@linkplain
1196 * #defaultForkJoinWorkerThreadFactory default thread factory},
1197 * no UncaughtExceptionHandler, and non-async LIFO processing mode.
1198 *
1199 * @throws SecurityException if a security manager exists and
1200 * the caller is not permitted to modify threads
1201 * because it does not hold {@link
1202 * java.lang.RuntimePermission}{@code ("modifyThread")}
1203 */
1204 public ForkJoinPool() {
1205 this(Runtime.getRuntime().availableProcessors(),
1206 defaultForkJoinWorkerThreadFactory, null, false);
1207 }
1208
1209 /**
1210 * Creates a {@code ForkJoinPool} with the indicated parallelism
1211 * level, the {@linkplain
1212 * #defaultForkJoinWorkerThreadFactory default thread factory},
1213 * no UncaughtExceptionHandler, and non-async LIFO processing mode.
1214 *
1215 * @param parallelism the parallelism level
1216 * @throws IllegalArgumentException if parallelism less than or
1217 * equal to zero, or greater than implementation limit
1218 * @throws SecurityException if a security manager exists and
1219 * the caller is not permitted to modify threads
1220 * because it does not hold {@link
1221 * java.lang.RuntimePermission}{@code ("modifyThread")}
1222 */
1223 public ForkJoinPool(int parallelism) {
1224 this(parallelism, defaultForkJoinWorkerThreadFactory, null, false);
1225 }
1226
1227 /**
1228 * Creates a {@code ForkJoinPool} with the given parameters.
1229 *
1230 * @param parallelism the parallelism level. For default value,
1231 * use {@link java.lang.Runtime#availableProcessors}.
1232 * @param factory the factory for creating new threads. For default value,
1233 * use {@link #defaultForkJoinWorkerThreadFactory}.
1234 * @param handler the handler for internal worker threads that
1235 * terminate due to unrecoverable errors encountered while executing
1236 * tasks. For default value, use {@code null}.
1237 * @param asyncMode if true,
1238 * establishes local first-in-first-out scheduling mode for forked
1239 * tasks that are never joined. This mode may be more appropriate
1240 * than default locally stack-based mode in applications in which
1241 * worker threads only process event-style asynchronous tasks.
1242 * For default value, use {@code false}.
1243 * @throws IllegalArgumentException if parallelism less than or
1244 * equal to zero, or greater than implementation limit
1245 * @throws NullPointerException if the factory is null
1246 * @throws SecurityException if a security manager exists and
1247 * the caller is not permitted to modify threads
1248 * because it does not hold {@link
1249 * java.lang.RuntimePermission}{@code ("modifyThread")}
1250 */
1251 public ForkJoinPool(int parallelism,
1252 ForkJoinWorkerThreadFactory factory,
1253 Thread.UncaughtExceptionHandler handler,
1254 boolean asyncMode) {
1255 checkPermission();
1256 if (factory == null)
1257 throw new NullPointerException();
1258 if (parallelism <= 0 || parallelism > MAX_WORKERS)
1259 throw new IllegalArgumentException();
1260 this.parallelism = parallelism;
1261 this.factory = factory;
1262 this.ueh = handler;
1263 this.locallyFifo = asyncMode;
1264 int arraySize = initialArraySizeFor(parallelism);
1265 this.workers = new ForkJoinWorkerThread[arraySize];
1266 this.submissionQueue = new LinkedTransferQueue<ForkJoinTask<?>>();
1267 this.workerLock = new ReentrantLock();
1268 this.termination = new Phaser(1);
1269 this.poolNumber = poolNumberGenerator.incrementAndGet();
1270 }
1271
1272 /**
1273 * Returns initial power of two size for workers array.
1274 * @param pc the initial parallelism level
1275 */
1276 private static int initialArraySizeFor(int pc) {
1277 // If possible, initially allocate enough space for one spare
1278 int size = pc < MAX_WORKERS ? pc + 1 : MAX_WORKERS;
1279 // See Hackers Delight, sec 3.2. We know MAX_WORKERS < (1 >>> 16)
1280 size |= size >>> 1;
1281 size |= size >>> 2;
1282 size |= size >>> 4;
1283 size |= size >>> 8;
1284 return size + 1;
1285 }
1286
1287 // Execution methods
1288
1289 /**
1290 * Common code for execute, invoke and submit
1291 */
1292 private <T> void doSubmit(ForkJoinTask<T> task) {
1293 if (task == null)
1294 throw new NullPointerException();
1295 if (runState >= SHUTDOWN)
1296 throw new RejectedExecutionException();
1297 submissionQueue.offer(task);
1298 int c; // try to increment event count -- CAS failure OK
1299 UNSAFE.compareAndSwapInt(this, eventCountOffset, c = eventCount, c+1);
1300 helpMaintainParallelism(); // create, start, or resume some workers
1301 }
1302
1303 /**
1304 * Performs the given task, returning its result upon completion.
1305 *
1306 * @param task the task
1307 * @return the task's result
1308 * @throws NullPointerException if the task is null
1309 * @throws RejectedExecutionException if the task cannot be
1310 * scheduled for execution
1311 */
1312 public <T> T invoke(ForkJoinTask<T> task) {
1313 doSubmit(task);
1314 return task.join();
1315 }
1316
1317 /**
1318 * Arranges for (asynchronous) execution of the given task.
1319 *
1320 * @param task the task
1321 * @throws NullPointerException if the task is null
1322 * @throws RejectedExecutionException if the task cannot be
1323 * scheduled for execution
1324 */
1325 public void execute(ForkJoinTask<?> task) {
1326 doSubmit(task);
1327 }
1328
1329 // AbstractExecutorService methods
1330
1331 /**
1332 * @throws NullPointerException if the task is null
1333 * @throws RejectedExecutionException if the task cannot be
1334 * scheduled for execution
1335 */
1336 public void execute(Runnable task) {
1337 ForkJoinTask<?> job;
1338 if (task instanceof ForkJoinTask<?>) // avoid re-wrap
1339 job = (ForkJoinTask<?>) task;
1340 else
1341 job = ForkJoinTask.adapt(task, null);
1342 doSubmit(job);
1343 }
1344
1345 /**
1346 * Submits a ForkJoinTask for execution.
1347 *
1348 * @param task the task to submit
1349 * @return the task
1350 * @throws NullPointerException if the task is null
1351 * @throws RejectedExecutionException if the task cannot be
1352 * scheduled for execution
1353 */
1354 public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
1355 doSubmit(task);
1356 return task;
1357 }
1358
1359 /**
1360 * @throws NullPointerException if the task is null
1361 * @throws RejectedExecutionException if the task cannot be
1362 * scheduled for execution
1363 */
1364 public <T> ForkJoinTask<T> submit(Callable<T> task) {
1365 ForkJoinTask<T> job = ForkJoinTask.adapt(task);
1366 doSubmit(job);
1367 return job;
1368 }
1369
1370 /**
1371 * @throws NullPointerException if the task is null
1372 * @throws RejectedExecutionException if the task cannot be
1373 * scheduled for execution
1374 */
1375 public <T> ForkJoinTask<T> submit(Runnable task, T result) {
1376 ForkJoinTask<T> job = ForkJoinTask.adapt(task, result);
1377 doSubmit(job);
1378 return job;
1379 }
1380
1381 /**
1382 * @throws NullPointerException if the task is null
1383 * @throws RejectedExecutionException if the task cannot be
1384 * scheduled for execution
1385 */
1386 public ForkJoinTask<?> submit(Runnable task) {
1387 ForkJoinTask<?> job;
1388 if (task instanceof ForkJoinTask<?>) // avoid re-wrap
1389 job = (ForkJoinTask<?>) task;
1390 else
1391 job = ForkJoinTask.adapt(task, null);
1392 doSubmit(job);
1393 return job;
1394 }
1395
1396 /**
1397 * @throws NullPointerException {@inheritDoc}
1398 * @throws RejectedExecutionException {@inheritDoc}
1399 */
1400 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
1401 ArrayList<ForkJoinTask<T>> forkJoinTasks =
1402 new ArrayList<ForkJoinTask<T>>(tasks.size());
1403 for (Callable<T> task : tasks)
1404 forkJoinTasks.add(ForkJoinTask.adapt(task));
1405 invoke(new InvokeAll<T>(forkJoinTasks));
1406
1407 @SuppressWarnings({"unchecked", "rawtypes"})
1408 List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks;
1409 return futures;
1410 }
1411
1412 static final class InvokeAll<T> extends RecursiveAction {
1413 final ArrayList<ForkJoinTask<T>> tasks;
1414 InvokeAll(ArrayList<ForkJoinTask<T>> tasks) { this.tasks = tasks; }
1415 public void compute() {
1416 try { invokeAll(tasks); }
1417 catch (Exception ignore) {}
1418 }
1419 private static final long serialVersionUID = -7914297376763021607L;
1420 }
1421
1422 /**
1423 * Returns the factory used for constructing new workers.
1424 *
1425 * @return the factory used for constructing new workers
1426 */
1427 public ForkJoinWorkerThreadFactory getFactory() {
1428 return factory;
1429 }
1430
1431 /**
1432 * Returns the handler for internal worker threads that terminate
1433 * due to unrecoverable errors encountered while executing tasks.
1434 *
1435 * @return the handler, or {@code null} if none
1436 */
1437 public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() {
1438 return ueh;
1439 }
1440
1441 /**
1442 * Returns the targeted parallelism level of this pool.
1443 *
1444 * @return the targeted parallelism level of this pool
1445 */
1446 public int getParallelism() {
1447 return parallelism;
1448 }
1449
1450 /**
1451 * Returns the number of worker threads that have started but not
1452 * yet terminated. The result returned by this method may differ
1453 * from {@link #getParallelism} when threads are created to
1454 * maintain parallelism when others are cooperatively blocked.
1455 *
1456 * @return the number of worker threads
1457 */
1458 public int getPoolSize() {
1459 return workerCounts >>> TOTAL_COUNT_SHIFT;
1460 }
1461
1462 /**
1463 * Returns {@code true} if this pool uses local first-in-first-out
1464 * scheduling mode for forked tasks that are never joined.
1465 *
1466 * @return {@code true} if this pool uses async mode
1467 */
1468 public boolean getAsyncMode() {
1469 return locallyFifo;
1470 }
1471
1472 /**
1473 * Returns an estimate of the number of worker threads that are
1474 * not blocked waiting to join tasks or for other managed
1475 * synchronization. This method may overestimate the
1476 * number of running threads.
1477 *
1478 * @return the number of worker threads
1479 */
1480 public int getRunningThreadCount() {
1481 return workerCounts & RUNNING_COUNT_MASK;
1482 }
1483
1484 /**
1485 * Returns an estimate of the number of threads that are currently
1486 * stealing or executing tasks. This method may overestimate the
1487 * number of active threads.
1488 *
1489 * @return the number of active threads
1490 */
1491 public int getActiveThreadCount() {
1492 return runState & ACTIVE_COUNT_MASK;
1493 }
1494
1495 /**
1496 * Returns {@code true} if all worker threads are currently idle.
1497 * An idle worker is one that cannot obtain a task to execute
1498 * because none are available to steal from other threads, and
1499 * there are no pending submissions to the pool. This method is
1500 * conservative; it might not return {@code true} immediately upon
1501 * idleness of all threads, but will eventually become true if
1502 * threads remain inactive.
1503 *
1504 * @return {@code true} if all threads are currently idle
1505 */
1506 public boolean isQuiescent() {
1507 return (runState & ACTIVE_COUNT_MASK) == 0;
1508 }
1509
1510 /**
1511 * Returns an estimate of the total number of tasks stolen from
1512 * one thread's work queue by another. The reported value
1513 * underestimates the actual total number of steals when the pool
1514 * is not quiescent. This value may be useful for monitoring and
1515 * tuning fork/join programs: in general, steal counts should be
1516 * high enough to keep threads busy, but low enough to avoid
1517 * overhead and contention across threads.
1518 *
1519 * @return the number of steals
1520 */
1521 public long getStealCount() {
1522 return stealCount;
1523 }
1524
1525 /**
1526 * Returns an estimate of the total number of tasks currently held
1527 * in queues by worker threads (but not including tasks submitted
1528 * to the pool that have not begun executing). This value is only
1529 * an approximation, obtained by iterating across all threads in
1530 * the pool. This method may be useful for tuning task
1531 * granularities.
1532 *
1533 * @return the number of queued tasks
1534 */
1535 public long getQueuedTaskCount() {
1536 long count = 0;
1537 for (ForkJoinWorkerThread w : workers)
1538 if (w != null)
1539 count += w.getQueueSize();
1540 return count;
1541 }
1542
1543 /**
1544 * Returns an estimate of the number of tasks submitted to this
1545 * pool that have not yet begun executing. This method takes time
1546 * proportional to the number of submissions.
1547 *
1548 * @return the number of queued submissions
1549 */
1550 public int getQueuedSubmissionCount() {
1551 return submissionQueue.size();
1552 }
1553
1554 /**
1555 * Returns {@code true} if there are any tasks submitted to this
1556 * pool that have not yet begun executing.
1557 *
1558 * @return {@code true} if there are any queued submissions
1559 */
1560 public boolean hasQueuedSubmissions() {
1561 return !submissionQueue.isEmpty();
1562 }
1563
1564 /**
1565 * Removes and returns the next unexecuted submission if one is
1566 * available. This method may be useful in extensions to this
1567 * class that re-assign work in systems with multiple pools.
1568 *
1569 * @return the next submission, or {@code null} if none
1570 */
1571 protected ForkJoinTask<?> pollSubmission() {
1572 return submissionQueue.poll();
1573 }
1574
1575 /**
1576 * Removes all available unexecuted submitted and forked tasks
1577 * from scheduling queues and adds them to the given collection,
1578 * without altering their execution status. These may include
1579 * artificially generated or wrapped tasks. This method is
1580 * designed to be invoked only when the pool is known to be
1581 * quiescent. Invocations at other times may not remove all
1582 * tasks. A failure encountered while attempting to add elements
1583 * to collection {@code c} may result in elements being in
1584 * neither, either or both collections when the associated
1585 * exception is thrown. The behavior of this operation is
1586 * undefined if the specified collection is modified while the
1587 * operation is in progress.
1588 *
1589 * @param c the collection to transfer elements into
1590 * @return the number of elements transferred
1591 */
1592 protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
1593 int count = submissionQueue.drainTo(c);
1594 for (ForkJoinWorkerThread w : workers)
1595 if (w != null)
1596 count += w.drainTasksTo(c);
1597 return count;
1598 }
1599
1600 /**
1601 * Returns a string identifying this pool, as well as its state,
1602 * including indications of run state, parallelism level, and
1603 * worker and task counts.
1604 *
1605 * @return a string identifying this pool, as well as its state
1606 */
1607 public String toString() {
1608 long st = getStealCount();
1609 long qt = getQueuedTaskCount();
1610 long qs = getQueuedSubmissionCount();
1611 int wc = workerCounts;
1612 int tc = wc >>> TOTAL_COUNT_SHIFT;
1613 int rc = wc & RUNNING_COUNT_MASK;
1614 int pc = parallelism;
1615 int rs = runState;
1616 int ac = rs & ACTIVE_COUNT_MASK;
1617 return super.toString() +
1618 "[" + runLevelToString(rs) +
1619 ", parallelism = " + pc +
1620 ", size = " + tc +
1621 ", active = " + ac +
1622 ", running = " + rc +
1623 ", steals = " + st +
1624 ", tasks = " + qt +
1625 ", submissions = " + qs +
1626 "]";
1627 }
1628
1629 private static String runLevelToString(int s) {
1630 return ((s & TERMINATED) != 0 ? "Terminated" :
1631 ((s & TERMINATING) != 0 ? "Terminating" :
1632 ((s & SHUTDOWN) != 0 ? "Shutting down" :
1633 "Running")));
1634 }
1635
1636 /**
1637 * Initiates an orderly shutdown in which previously submitted
1638 * tasks are executed, but no new tasks will be accepted.
1639 * Invocation has no additional effect if already shut down.
1640 * Tasks that are in the process of being submitted concurrently
1641 * during the course of this method may or may not be rejected.
1642 *
1643 * @throws SecurityException if a security manager exists and
1644 * the caller is not permitted to modify threads
1645 * because it does not hold {@link
1646 * java.lang.RuntimePermission}{@code ("modifyThread")}
1647 */
1648 public void shutdown() {
1649 checkPermission();
1650 advanceRunLevel(SHUTDOWN);
1651 tryTerminate(false);
1652 }
1653
1654 /**
1655 * Attempts to cancel and/or stop all tasks, and reject all
1656 * subsequently submitted tasks. Tasks that are in the process of
1657 * being submitted or executed concurrently during the course of
1658 * this method may or may not be rejected. This method cancels
1659 * both existing and unexecuted tasks, in order to permit
1660 * termination in the presence of task dependencies. So the method
1661 * always returns an empty list (unlike the case for some other
1662 * Executors).
1663 *
1664 * @return an empty list
1665 * @throws SecurityException if a security manager exists and
1666 * the caller is not permitted to modify threads
1667 * because it does not hold {@link
1668 * java.lang.RuntimePermission}{@code ("modifyThread")}
1669 */
1670 public List<Runnable> shutdownNow() {
1671 checkPermission();
1672 tryTerminate(true);
1673 return Collections.emptyList();
1674 }
1675
1676 /**
1677 * Returns {@code true} if all tasks have completed following shut down.
1678 *
1679 * @return {@code true} if all tasks have completed following shut down
1680 */
1681 public boolean isTerminated() {
1682 return runState >= TERMINATED;
1683 }
1684
1685 /**
1686 * Returns {@code true} if the process of termination has
1687 * commenced but not yet completed. This method may be useful for
1688 * debugging. A return of {@code true} reported a sufficient
1689 * period after shutdown may indicate that submitted tasks have
1690 * ignored or suppressed interruption, causing this executor not
1691 * to properly terminate.
1692 *
1693 * @return {@code true} if terminating but not yet terminated
1694 */
1695 public boolean isTerminating() {
1696 return (runState & (TERMINATING|TERMINATED)) == TERMINATING;
1697 }
1698
1699 /**
1700 * Returns {@code true} if this pool has been shut down.
1701 *
1702 * @return {@code true} if this pool has been shut down
1703 */
1704 public boolean isShutdown() {
1705 return runState >= SHUTDOWN;
1706 }
1707
1708 /**
1709 * Blocks until all tasks have completed execution after a shutdown
1710 * request, or the timeout occurs, or the current thread is
1711 * interrupted, whichever happens first.
1712 *
1713 * @param timeout the maximum time to wait
1714 * @param unit the time unit of the timeout argument
1715 * @return {@code true} if this executor terminated and
1716 * {@code false} if the timeout elapsed before termination
1717 * @throws InterruptedException if interrupted while waiting
1718 */
1719 public boolean awaitTermination(long timeout, TimeUnit unit)
1720 throws InterruptedException {
1721 try {
1722 return termination.awaitAdvanceInterruptibly(0, timeout, unit) > 0;
1723 } catch (TimeoutException ex) {
1724 return false;
1725 }
1726 }
1727
1728 /**
1729 * Interface for extending managed parallelism for tasks running
1730 * in {@link ForkJoinPool}s.
1731 *
1732 * <p>A {@code ManagedBlocker} provides two methods. Method
1733 * {@code isReleasable} must return {@code true} if blocking is
1734 * not necessary. Method {@code block} blocks the current thread
1735 * if necessary (perhaps internally invoking {@code isReleasable}
1736 * before actually blocking). The unusual methods in this API
1737 * accommodate synchronizers that may, but don't usually, block
1738 * for long periods. Similarly, they allow more efficient internal
1739 * handling of cases in which additional workers may be, but
1740 * usually are not, needed to ensure sufficient parallelism.
1741 * Toward this end, implementations of method {@code isReleasable}
1742 * must be amenable to repeated invocation.
1743 *
1744 * <p>For example, here is a ManagedBlocker based on a
1745 * ReentrantLock:
1746 * <pre> {@code
1747 * class ManagedLocker implements ManagedBlocker {
1748 * final ReentrantLock lock;
1749 * boolean hasLock = false;
1750 * ManagedLocker(ReentrantLock lock) { this.lock = lock; }
1751 * public boolean block() {
1752 * if (!hasLock)
1753 * lock.lock();
1754 * return true;
1755 * }
1756 * public boolean isReleasable() {
1757 * return hasLock || (hasLock = lock.tryLock());
1758 * }
1759 * }}</pre>
1760 *
1761 * <p>Here is a class that possibly blocks waiting for an
1762 * item on a given queue:
1763 * <pre> {@code
1764 * class QueueTaker<E> implements ManagedBlocker {
1765 * final BlockingQueue<E> queue;
1766 * volatile E item = null;
1767 * QueueTaker(BlockingQueue<E> q) { this.queue = q; }
1768 * public boolean block() throws InterruptedException {
1769 * if (item == null)
1770 * item = queue.take();
1771 * return true;
1772 * }
1773 * public boolean isReleasable() {
1774 * return item != null || (item = queue.poll()) != null;
1775 * }
1776 * public E getItem() { // call after pool.managedBlock completes
1777 * return item;
1778 * }
1779 * }}</pre>
1780 */
1781 public static interface ManagedBlocker {
1782 /**
1783 * Possibly blocks the current thread, for example waiting for
1784 * a lock or condition.
1785 *
1786 * @return {@code true} if no additional blocking is necessary
1787 * (i.e., if isReleasable would return true)
1788 * @throws InterruptedException if interrupted while waiting
1789 * (the method is not required to do so, but is allowed to)
1790 */
1791 boolean block() throws InterruptedException;
1792
1793 /**
1794 * Returns {@code true} if blocking is unnecessary.
1795 */
1796 boolean isReleasable();
1797 }
1798
1799 /**
1800 * Blocks in accord with the given blocker. If the current thread
1801 * is a {@link ForkJoinWorkerThread}, this method possibly
1802 * arranges for a spare thread to be activated if necessary to
1803 * ensure sufficient parallelism while the current thread is blocked.
1804 *
1805 * <p>If the caller is not a {@link ForkJoinTask}, this method is
1806 * behaviorally equivalent to
1807 * <pre> {@code
1808 * while (!blocker.isReleasable())
1809 * if (blocker.block())
1810 * return;
1811 * }</pre>
1812 *
1813 * If the caller is a {@code ForkJoinTask}, then the pool may
1814 * first be expanded to ensure parallelism, and later adjusted.
1815 *
1816 * @param blocker the blocker
1817 * @throws InterruptedException if blocker.block did so
1818 */
1819 public static void managedBlock(ManagedBlocker blocker)
1820 throws InterruptedException {
1821 Thread t = Thread.currentThread();
1822 if (t instanceof ForkJoinWorkerThread) {
1823 ForkJoinWorkerThread w = (ForkJoinWorkerThread) t;
1824 w.pool.awaitBlocker(blocker);
1825 }
1826 else {
1827 do {} while (!blocker.isReleasable() && !blocker.block());
1828 }
1829 }
1830
1831 // AbstractExecutorService overrides. These rely on undocumented
1832 // fact that ForkJoinTask.adapt returns ForkJoinTasks that also
1833 // implement RunnableFuture.
1834
1835 protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
1836 return (RunnableFuture<T>) ForkJoinTask.adapt(runnable, value);
1837 }
1838
1839 protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
1840 return (RunnableFuture<T>) ForkJoinTask.adapt(callable);
1841 }
1842
1843 // Unsafe mechanics
1844
1845 private static final sun.misc.Unsafe UNSAFE = getUnsafe();
1846 private static final long workerCountsOffset =
1847 objectFieldOffset("workerCounts", ForkJoinPool.class);
1848 private static final long runStateOffset =
1849 objectFieldOffset("runState", ForkJoinPool.class);
1850 private static final long eventCountOffset =
1851 objectFieldOffset("eventCount", ForkJoinPool.class);
1852 private static final long eventWaitersOffset =
1853 objectFieldOffset("eventWaiters", ForkJoinPool.class);
1854 private static final long stealCountOffset =
1855 objectFieldOffset("stealCount", ForkJoinPool.class);
1856 private static final long spareWaitersOffset =
1857 objectFieldOffset("spareWaiters", ForkJoinPool.class);
1858
1859 private static long objectFieldOffset(String field, Class<?> klazz) {
1860 try {
1861 return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
1862 } catch (NoSuchFieldException e) {
1863 // Convert Exception to corresponding Error
1864 NoSuchFieldError error = new NoSuchFieldError(field);
1865 error.initCause(e);
1866 throw error;
1867 }
1868 }
1869
1870 /**
1871 * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
1872 * Replace with a simple call to Unsafe.getUnsafe when integrating
1873 * into a jdk.
1874 *
1875 * @return a sun.misc.Unsafe
1876 */
1877 private static sun.misc.Unsafe getUnsafe() {
1878 try {
1879 return sun.misc.Unsafe.getUnsafe();
1880 } catch (SecurityException se) {
1881 try {
1882 return java.security.AccessController.doPrivileged
1883 (new java.security
1884 .PrivilegedExceptionAction<sun.misc.Unsafe>() {
1885 public sun.misc.Unsafe run() throws Exception {
1886 java.lang.reflect.Field f = sun.misc
1887 .Unsafe.class.getDeclaredField("theUnsafe");
1888 f.setAccessible(true);
1889 return (sun.misc.Unsafe) f.get(null);
1890 }});
1891 } catch (java.security.PrivilegedActionException e) {
1892 throw new RuntimeException("Could not initialize intrinsics",
1893 e.getCause());
1894 }
1895 }
1896 }
1897 }