public class BlockedThreadPoolExecutor extends ThreadPoolExecutor {

    private final Semaphore semaphore;

    public BlockedThreadPoolExecutor(int poolSize) {
        super(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<>());
        semaphore = new Semaphore(poolSize);
    }
    
    @Override
    public void execute(Runnable command) {
        try {
            semaphore.acquire();
            super.execute(command);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        semaphore.release();
    }
}

 

相关文章:

  • 2021-10-19
  • 2022-12-23
  • 2021-12-24
  • 2021-10-07
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-10
  • 2021-11-22
  • 2021-07-08
  • 2021-11-25
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案