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
Post a Comment