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

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -