Stay organized with collections
Save and categorize content based on your preferences.
ManagedBlocker
interface ManagedBlocker
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 java.util.concurrent.ForkJoinPool#managedBlock(java.util.concurrent.ForkJoinPool.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. Neither method is invoked after a prior invocation of isReleasable
or block
returns true
.
For example, here is a ManagedBlocker based on a ReentrantLock:
<code>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());
}
}</code>
Here is a class that possibly blocks waiting for an item on a given queue:
<code>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;
}
}</code>
Summary
Public methods |
abstract Boolean |
Possibly blocks the current thread, for example waiting for a lock or condition.
|
abstract Boolean |
Returns true if blocking is unnecessary.
|
Public methods
block
abstract fun block(): Boolean
Possibly blocks the current thread, for example waiting for a lock or condition.
Return |
Boolean |
true if no additional blocking is necessary (i.e., if isReleasable would return true) |
Exceptions |
java.lang.InterruptedException |
if interrupted while waiting (the method is not required to do so, but is allowed to) |
isReleasable
abstract fun isReleasable(): Boolean
Returns true
if blocking is unnecessary.
Return |
Boolean |
true if blocking is unnecessary |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# ForkJoinPool.ManagedBlocker\n\nAdded in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nManagedBlocker\n==============\n\n```\ninterface ManagedBlocker\n```\n\n|-------------------------------------------------------|\n| [java.util.concurrent.ForkJoinPool.ManagedBlocker](#) |\n\nInterface for extending managed parallelism for tasks running in [ForkJoinPool](/reference/kotlin/java/util/concurrent/ForkJoinPool)s.\n\nA `ManagedBlocker` provides two methods. Method [isReleasable](#isReleasable()) must return `true` if blocking is not necessary. Method [block](#block()) blocks the current thread if necessary (perhaps internally invoking `isReleasable` before actually blocking). These actions are performed by any thread invoking [java.util.concurrent.ForkJoinPool#managedBlock(java.util.concurrent.ForkJoinPool.ManagedBlocker)](/reference/kotlin/java/util/concurrent/ForkJoinPool#managedBlock(java.util.concurrent.ForkJoinPool.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. Neither method is invoked after a prior invocation of `isReleasable` or `block` returns `true`.\n\nFor example, here is a ManagedBlocker based on a ReentrantLock: \n\n```kotlin\n\u003ccode\u003eclass 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 }\u003c/code\u003e\n```\n\nHere is a class that possibly blocks waiting for an item on a given queue: \n\n```kotlin\n\u003ccode\u003eclass QueueTaker<E> implements ManagedBlocker {\n final BlockingQueue<E> queue;\n volatile E item = null;\n QueueTaker(BlockingQueue<E> 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 }\u003c/code\u003e\n```\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [block](#block())`()` Possibly blocks the current thread, for example waiting for a lock or condition. |\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isReleasable](#isReleasable())`()` Returns `true` if blocking is unnecessary. |\n\nPublic methods\n--------------\n\n### block\n\nAdded in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun block(): Boolean\n```\n\nPossibly blocks the current thread, for example waiting for a lock or condition.\n\n| Return ||\n|------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if no additional blocking is necessary (i.e., if isReleasable would return true) |\n\n| Exceptions ||\n|----------------------------------|---------------------------------------------------------------------------------------|\n| `java.lang.InterruptedException` | if interrupted while waiting (the method is not required to do so, but is allowed to) |\n\n### isReleasable\n\nAdded in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun isReleasable(): Boolean\n```\n\nReturns `true` if blocking is unnecessary.\n\n| Return ||\n|------------------------------------------------------------------------------------|-----------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if blocking is unnecessary |"]]