Interface for extending managed parallelism for tasks running
in {@link ForkJoinPool}s.
<p>A {@code ManagedBlocker} provides two methods. Method
{@link #isReleasable} must return {@code true} if blocking is
not necessary. Method {@link #block} blocks the current thread
if necessary (perhaps internally invoking {@code isReleasable}
before actually blocking). These actions are performed by any
thread invoking {@link 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 {@code isReleasable} must be amenable
to repeated invocation.
<p>For example, here is a ManagedBlocker based on a
ReentrantLock:
<pre> {@code
class ManagedLocker implements ManagedBlocker {
final ReentrantLock lock;
bool hasLock = false;
ManagedLocker(ReentrantLock lock) { this.lock = lock; }
bool block() {
if (!hasLock)
lock.lock();
return true;
}
bool isReleasable() {
return hasLock || (hasLock = lock.tryLock());
}
}}</pre>
<p>Here is a class that possibly blocks waiting for an
item on a given queue:
<pre> {@code
class QueueTaker!(E) : ManagedBlocker {
final BlockingQueue!(E) queue;
E item = null;
QueueTaker(BlockingQueue!(E) q) { this.queue = q; }
bool block() {
if (item is null)
item = queue.take();
return true;
}
bool isReleasable() {
return item !is null || (item = queue.poll()) !is null;
}
E getItem() { // call after pool.managedBlock completes
return item;
}
}}</pre>
Interface for extending managed parallelism for tasks running in {@link ForkJoinPool}s.
<p>A {@code ManagedBlocker} provides two methods. Method {@link #isReleasable} must return {@code true} if blocking is not necessary. Method {@link #block} blocks the current thread if necessary (perhaps internally invoking {@code isReleasable} before actually blocking). These actions are performed by any thread invoking {@link 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 {@code isReleasable} must be amenable to repeated invocation.
<p>For example, here is a ManagedBlocker based on a ReentrantLock: <pre> {@code class ManagedLocker implements ManagedBlocker { final ReentrantLock lock; bool hasLock = false; ManagedLocker(ReentrantLock lock) { this.lock = lock; } bool block() { if (!hasLock) lock.lock(); return true; } bool isReleasable() { return hasLock || (hasLock = lock.tryLock()); } }}</pre>
<p>Here is a class that possibly blocks waiting for an item on a given queue: <pre> {@code class QueueTaker!(E) : ManagedBlocker { final BlockingQueue!(E) queue; E item = null; QueueTaker(BlockingQueue!(E) q) { this.queue = q; } bool block() { if (item is null) item = queue.take(); return true; } bool isReleasable() { return item !is null || (item = queue.poll()) !is null; } E getItem() { // call after pool.managedBlock completes return item; } }}</pre>