ForkJoinPool.ManagedBlocker
Stay organized with collections
Save and categorize content based on your preferences.
Interface for extending managed parallelism for tasks running
in ForkJoinPool
s.
A ManagedBlocker
provides two methods. Method
isReleasable()
must return true
if blocking is
not necessary. Method block()
blocks the current thread
if necessary (perhaps internally invoking isReleasable
before actually blocking). These actions are performed by any
thread invoking ForkJoinPool.managedBlock(ManagedBlocker)
.
The unusual methods in this API accommodate synchronizers that
may, but don't usually, block for long periods. Similarly, they
allow more efficient internal handling of cases in which
additional workers may be, but usually are not, needed to
ensure sufficient parallelism. Toward this end,
implementations of method isReleasable
must be amenable
to repeated invocation.
For example, here is a ManagedBlocker based on a
ReentrantLock:
class ManagedLocker implements ManagedBlocker {
final ReentrantLock lock;
boolean hasLock = false;
ManagedLocker(ReentrantLock lock) { this.lock = lock; }
public boolean block() {
if (!hasLock)
lock.lock();
return true;
}
public boolean isReleasable() {
return hasLock || (hasLock = lock.tryLock());
}
}
Here is a class that possibly blocks waiting for an
item on a given queue:
class QueueTaker<E> implements ManagedBlocker {
final BlockingQueue<E> queue;
volatile E item = null;
QueueTaker(BlockingQueue<E> q) { this.queue = q; }
public boolean block() throws InterruptedException {
if (item == null)
item = queue.take();
return true;
}
public boolean isReleasable() {
return item != null || (item = queue.poll()) != null;
}
public E getItem() { // call after pool.managedBlock completes
return item;
}
}
Public Method Summary
abstract
boolean
|
block()
Possibly blocks the current thread, for example waiting for
a lock or condition.
|
abstract
boolean
|
|
Public Methods
public
abstract
boolean
block
()
Possibly blocks the current thread, for example waiting for
a lock or condition.
Returns
true
if no additional blocking is necessary
(i.e., if isReleasable would return true)
Throws
InterruptedException |
if interrupted while waiting
(the method is not required to do so, but is allowed to)
|
public
abstract
boolean
isReleasable
()
Returns true
if blocking is unnecessary.
Returns
true
if blocking is unnecessary
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eForkJoinPool.ManagedBlocker\u003c/code\u003e is designed to extend managed parallelism for tasks running in \u003ccode\u003eForkJoinPool\u003c/code\u003es by providing a mechanism for tasks to potentially block without hindering overall throughput.\u003c/p\u003e\n"],["\u003cp\u003eIt offers two key methods: \u003ccode\u003eisReleasable()\u003c/code\u003e which checks if blocking is necessary, and \u003ccode\u003eblock()\u003c/code\u003e which blocks the current thread if required.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eForkJoinPool.managedBlock(ManagedBlocker)\u003c/code\u003e utilizes these methods to manage task execution efficiently, enabling potential blocking while ensuring parallelism.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code examples demonstrate how \u003ccode\u003eManagedBlocker\u003c/code\u003e can be implemented for tasks involving locks and queues, illustrating its practical application.\u003c/p\u003e\n"]]],["`ForkJoinPool.ManagedBlocker` extends managed parallelism for tasks in `ForkJoinPool`s. It has two methods: `isReleasable()` checks if blocking is needed, returning `true` if not; `block()` blocks the thread if necessary, potentially calling `isReleasable` internally. These actions occur via `ForkJoinPool.managedBlock(ManagedBlocker)`. Implementations of `isReleasable` must allow repeated invocation, and `block` returns `true` if no further blocking is required.\n"],null,["# ForkJoinPool.ManagedBlocker\n\npublic static interface **ForkJoinPool.ManagedBlocker** \nInterface for extending managed parallelism for tasks running\nin [ForkJoinPool](../../../../reference/java/util/concurrent/ForkJoinPool.html)s.\n\nA `ManagedBlocker` provides two methods. Method\n[isReleasable()](../../../../reference/java/util/concurrent/ForkJoinPool.ManagedBlocker.html#isReleasable()) must return `true` if blocking is\nnot necessary. Method [block()](../../../../reference/java/util/concurrent/ForkJoinPool.ManagedBlocker.html#block()) blocks the current thread\nif necessary (perhaps internally invoking `isReleasable`\nbefore actually blocking). These actions are performed by any\nthread invoking [ForkJoinPool.managedBlock(ManagedBlocker)](../../../../reference/java/util/concurrent/ForkJoinPool.html#managedBlock(java.util.concurrent.ForkJoinPool.ManagedBlocker)).\nThe unusual methods in this API accommodate synchronizers that\nmay, but don't usually, block for long periods. Similarly, they\nallow more efficient internal handling of cases in which\nadditional workers may be, but usually are not, needed to\nensure sufficient parallelism. Toward this end,\nimplementations of method `isReleasable` must be amenable\nto repeated invocation.\n\nFor example, here is a ManagedBlocker based on a\nReentrantLock: \n\n class ManagedLocker implements ManagedBlocker {\n final ReentrantLock lock;\n boolean hasLock = false;\n ManagedLocker(ReentrantLock lock) { this.lock = lock; }\n public boolean block() {\n if (!hasLock)\n lock.lock();\n return true;\n }\n public boolean isReleasable() {\n return hasLock || (hasLock = lock.tryLock());\n }\n }\n\nHere is a class that possibly blocks waiting for an\nitem on a given queue: \n\n class QueueTaker\u003cE\u003e implements ManagedBlocker {\n final BlockingQueue\u003cE\u003e queue;\n volatile E item = null;\n QueueTaker(BlockingQueue\u003cE\u003e q) { this.queue = q; }\n public boolean block() throws InterruptedException {\n if (item == null)\n item = queue.take();\n return true;\n }\n public boolean isReleasable() {\n return item != null || (item = queue.poll()) != null;\n }\n public E getItem() { // call after pool.managedBlock completes\n return item;\n }\n }\n\n\u003cbr /\u003e\n\n### Public Method Summary\n\n|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract boolean | [block](../../../../reference/java/util/concurrent/ForkJoinPool.ManagedBlocker.html#block())() Possibly blocks the current thread, for example waiting for a lock or condition. |\n| abstract boolean | [isReleasable](../../../../reference/java/util/concurrent/ForkJoinPool.ManagedBlocker.html#isReleasable())() Returns `true` if blocking is unnecessary. |\n\nPublic Methods\n--------------\n\n#### public abstract boolean\n**block**\n()\n\nPossibly blocks the current thread, for example waiting for\na lock or condition. \n\n##### Returns\n\n- `true` if no additional blocking is necessary (i.e., if isReleasable would return true) \n\n##### Throws\n\n| [InterruptedException](../../../../reference/java/lang/InterruptedException.html) | if interrupted while waiting (the method is not required to do so, but is allowed to) |\n|-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|\n\n#### public abstract boolean\n**isReleasable**\n()\n\nReturns `true` if blocking is unnecessary. \n\n##### Returns\n\n- `true` if blocking is unnecessary"]]