1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 想通过使用信号量来控制提交的任务,设置了最大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 |