想通过使用信号量来控制提交的任务,设置了最大size为10,信号量也设为了10,但是任务还是会被拒绝
以下是代码
public class BoundedExecutor {
private final ExecutorService executorService;
private final Semaphore semaphore;
public BoundedExecutor(ExecutorService executorService, Semaphore semaphore) {
this.executorService = executorService;
this.semaphore = semaphore;
}
public void execute(Runnable runnable) throws InterruptedException {
semaphore.acquire();
executorService.execute(() -> {
try {
runnable.run();
}finally {
semaphore.release();
}
});
}
}
ExecutorService executorService1 = new ThreadPoolExecutor(1,10,60L, TimeUnit.SECONDS, new SynchronousQueue<>());
BoundedExecutor boundedExecutor = new BoundedExecutor(executorService1,new Semaphore(10));
for (int i = 0;i <10000;i++){
boundedExecutor.execute(runnable);
}
executorS