java - Is this code snippet about semaphore necessary? -


here's code java concurrency in practice, showing how make execute block when work queue full using semaphore bound task injection rate. semaphore equal pool size plus number of queued tasks want allow.

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();                     } {                     semaphore.release();                     }                 }             });         } catch (rejectedexecutionexception e) {             semaphore.release();         }     } } 

my question about

catch (rejectedexecutionexception e) { semaphore.release(); }

isn't unnecessary while have semaphore.acquire(); above?

if work queue full 'semaphore.acquire' should block, , there no rejectedexecutionexception.

the documentation says throws rejectedexecutionexception if task cannot accepted execution. want semaphore released if task can't accepted any reason.


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -