ExecutorService that executes tasks sequentially but takes threads from a pool

后端 未结 6 1739
孤城傲影
孤城傲影 2021-02-20 11:22

I am trying to build an implementation of the ExecutorService, let\'s call it SequentialPooledExecutor, with the following properties.

6条回答
  •  你的背包
    2021-02-20 11:36

    If you want to configure bounded queue, use ThreadPoolExecutor

    ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
    TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory,
    RejectedExecutionHandler handler)
    

    For your use case, use ThreadPoolExecutor as

    ThreadPoolExecutor executor =    
    ThreadPoolExecutor(1,1,60,TimeUnit.SECONDS,new ArrayBlockingQueue(1000));
    

    Above code caps size of queue is ThreadPoolExecutor as 1000. If you want to use custom rejected execution handler, you can configure RejectedExeutionHandler.

    Related SE question:

    How to properly use Java Executor?

提交回复
热议问题