Skip to content

Most visited

Recently visited

added in API level 21


public static interface ForkJoinPool.ManagedBlocker


Interface for extending managed parallelism for tasks running in ForkJoinPools.

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

abstract boolean block()

Possibly blocks the current thread, for example waiting for a lock or condition.

abstract boolean isReleasable()

Returns true if blocking is unnecessary.

Public methods


added in API level 21
boolean block ()

Possibly blocks the current thread, for example waiting for a lock or condition.

boolean true if no additional blocking is necessary (i.e., if isReleasable would return true)

InterruptedException if interrupted while waiting (the method is not required to do so, but is allowed to)


added in API level 21
boolean isReleasable ()

Returns true if blocking is unnecessary.

boolean true if blocking is unnecessary

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)