Difference between Executors.newFixedThreadPool(1) and Executors.newSingleThreadExecutor()

后端 未结 4 886
傲寒
傲寒 2020-12-29 23:12

My question is : does it make sense to use Executors.newFixedThreadPool(1)??. In two threads (main + oneAnotherThread) scenarios is it efficient to use execut

4条回答
  •  伪装坚强ぢ
    2020-12-30 00:01

    Sometimes need to use Executors.newFixedThreadPool(1) to determine number of tasks in the queue

    private final ExecutorService executor = Executors.newFixedThreadPool(1);
    
    public int getTaskInQueueCount() {
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
        return threadPoolExecutor.getQueue().size();
    }
    

提交回复
热议问题