205 |
|
|
206 |
|
/** |
207 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
208 |
+ |
* |
209 |
|
* @param pool the pool this thread works in |
210 |
|
* @throws NullPointerException if pool is null |
211 |
|
*/ |
219 |
|
// Public access methods |
220 |
|
|
221 |
|
/** |
222 |
< |
* Returns the pool hosting this thread |
222 |
> |
* Returns the pool hosting this thread. |
223 |
> |
* |
224 |
|
* @return the pool |
225 |
|
*/ |
226 |
|
public ForkJoinPool getPool() { |
233 |
|
* threads (minus one) that have ever been created in the pool. |
234 |
|
* This method may be useful for applications that track status or |
235 |
|
* collect results per-worker rather than per-task. |
236 |
< |
* @return the index number. |
236 |
> |
* |
237 |
> |
* @return the index number |
238 |
|
*/ |
239 |
|
public int getPoolIndex() { |
240 |
|
return poolIndex; |
243 |
|
/** |
244 |
|
* Establishes local first-in-first-out scheduling mode for forked |
245 |
|
* tasks that are never joined. |
246 |
+ |
* |
247 |
|
* @param async if true, use locally FIFO scheduling |
248 |
|
*/ |
249 |
|
void setAsyncMode(boolean async) { |
265 |
|
final boolean shutdownNow() { return transitionRunStateTo(TERMINATING); } |
266 |
|
|
267 |
|
/** |
268 |
< |
* Transition to at least the given state. Return true if not |
269 |
< |
* already at least given state. |
268 |
> |
* Transitions to at least the given state. Returns true if not |
269 |
> |
* already at least at given state. |
270 |
|
*/ |
271 |
|
private boolean transitionRunStateTo(int state) { |
272 |
|
for (;;) { |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* Try to set status to active; fail on contention |
282 |
> |
* Tries to set status to active; fails on contention. |
283 |
|
*/ |
284 |
|
private boolean tryActivate() { |
285 |
|
if (!active) { |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* Try to set status to active; fail on contention |
294 |
> |
* Tries to set status to active; fails on contention. |
295 |
|
*/ |
296 |
|
private boolean tryInactivate() { |
297 |
|
if (active) { |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* Computes next value for random victim probe. Scans don't |
306 |
> |
* Computes next value for random victim probe. Scans don't |
307 |
|
* require a very high quality generator, but also not a crummy |
308 |
< |
* one. Marsaglia xor-shift is cheap and works well. |
308 |
> |
* one. Marsaglia xor-shift is cheap and works well. |
309 |
|
*/ |
310 |
|
private static int xorShift(int r) { |
311 |
|
r ^= r << 1; |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* Execute tasks until shut down. |
338 |
> |
* Executes tasks until shut down. |
339 |
|
*/ |
340 |
|
private void mainLoop() { |
341 |
|
while (!isShutdown()) { |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* Perform cleanup associated with termination of this worker |
370 |
> |
* Performs cleanup associated with termination of this worker |
371 |
|
* thread. If you override this method, you must invoke |
372 |
|
* super.onTermination at the end of the overridden method. |
373 |
|
* |
374 |
|
* @param exception the exception causing this thread to abort due |
375 |
< |
* to an unrecoverable error, or null if completed normally. |
375 |
> |
* to an unrecoverable error, or null if completed normally |
376 |
|
*/ |
377 |
|
protected void onTermination(Throwable exception) { |
378 |
|
// Execute remaining local tasks unless aborting or terminating |
432 |
|
|
433 |
|
/** |
434 |
|
* Pushes a task. Called only by current thread. |
435 |
+ |
* |
436 |
|
* @param t the task. Caller must ensure non-null. |
437 |
|
*/ |
438 |
|
final void pushTask(ForkJoinTask<?> t) { |
450 |
|
/** |
451 |
|
* Tries to take a task from the base of the queue, failing if |
452 |
|
* either empty or contended. |
453 |
< |
* @return a task, or null if none or contended. |
453 |
> |
* |
454 |
> |
* @return a task, or null if none or contended |
455 |
|
*/ |
456 |
|
final ForkJoinTask<?> deqTask() { |
457 |
|
ForkJoinTask<?> t; |
493 |
|
* Specialized version of popTask to pop only if |
494 |
|
* topmost element is the given task. Called only |
495 |
|
* by current thread while active. |
496 |
< |
* @param t the task. Caller must ensure non-null |
496 |
> |
* |
497 |
> |
* @param t the task. Caller must ensure non-null. |
498 |
|
*/ |
499 |
|
final boolean unpushTask(ForkJoinTask<?> t) { |
500 |
|
ForkJoinTask<?>[] q = queue; |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
< |
* gets and removes a local or stolen a task |
597 |
> |
* Gets and removes a local or stolen task. |
598 |
> |
* |
599 |
|
* @return a task, if available |
600 |
|
*/ |
601 |
|
final ForkJoinTask<?> pollTask() { |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* gets a local task |
609 |
> |
* Gets a local task. |
610 |
> |
* |
611 |
|
* @return a task, if available |
612 |
|
*/ |
613 |
|
final ForkJoinTask<?> pollLocalTask() { |
616 |
|
|
617 |
|
/** |
618 |
|
* Returns a pool submission, if one exists, activating first. |
619 |
+ |
* |
620 |
|
* @return a submission, if available |
621 |
|
*/ |
622 |
|
private ForkJoinTask<?> pollSubmission() { |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* Drains tasks to given collection c |
645 |
> |
* Drains tasks to given collection c. |
646 |
> |
* |
647 |
|
* @return the number of tasks drained |
648 |
|
*/ |
649 |
|
final int drainTasksTo(Collection<ForkJoinTask<?>> c) { |
657 |
|
} |
658 |
|
|
659 |
|
/** |
660 |
< |
* Get and clear steal count for accumulation by pool. Called |
660 |
> |
* Gets and clears steal count for accumulation by pool. Called |
661 |
|
* only when known to be idle (in pool.sync and termination). |
662 |
|
*/ |
663 |
|
final int getAndClearStealCount() { |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* Scan, returning early if joinMe done |
708 |
> |
* Scans, returning early if joinMe done |
709 |
|
*/ |
710 |
|
final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) { |
711 |
|
ForkJoinTask<?> t = pollTask(); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
720 |
< |
* Runs tasks until pool isQuiescent |
720 |
> |
* Runs tasks until pool isQuiescent. |
721 |
|
*/ |
722 |
|
final void helpQuiescePool() { |
723 |
|
for (;;) { |