Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Concurrency in Practice: BoundedExecutor implementation
    primarykey
    data
    text
    <p>This is the implementation of the BoundedExecutor class in the Java Concurrency in Practice book:</p> <pre><code>public class BoundedExecutor { private final Executor exec; private final Semaphore semaphore; public BoundedExecutor(Executor exec, int bound) { this.exec = exec; this.semaphore = new Semaphore(bound); } public void submitTask(final Runnable command) throws InterruptedException { semaphore.acquire(); try { exec.execute(new Runnable() { public void run() { try { command.run(); } finally { semaphore.release(); } } }); } catch (RejectedExecutionException e) { semaphore.release(); } } } </code></pre> <p>Is there a reason why the RejectedExecutionException is being caught instead of letting it propagate further? In this case, if the task is rejected, whoever submits the task will be none the wiser.</p> <p>Isn't is better to just replace the catch-block with a finally-block?</p> <p>This is my implementation of a BoundedExecutor that accepts a Callable instead of a Runnable:</p> <pre><code>public class BoundedExecutor { private final ExecutorService exec; private final Semaphore semaphore; public BoundedExecutor(ExecutorService exec, int bound) { this.exec = exec; this.semaphore = new Semaphore(bound); } public &lt;V&gt; Future&lt;V&gt; submitTask(final Callable&lt;V&gt; command) throws InterruptedException { semaphore.acquire(); try { return exec.submit(new Callable&lt;V&gt;() { @Override public V call() throws Exception { try { return command.call(); } finally { semaphore.release(); } } }); } catch (RejectedExecutionException e) { semaphore.release(); throw e; } } } </code></pre> <p>Is it a correct implementation?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload