Search code examples
runnablejava.util.concurrentcallablethreadpoolexecutor

ThreadPoolExecutor and ArrayBlockingQueue


Excuse me for my bad english. When the number of threads in the pool to rise above 10, the the task will be placed in ArrayBlockingQueue . But if the task Callable? Constructor ThreadPoolExecutor not accept ArrayBlockingQueue typed as Callable. How, then, will be added to the queue the task?

ExecutorService executorService = new ThreadPoolExecutor(2, 10,
                60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(100));

Solution

  • What happens is that ThreadPoolExecutor does not add your Callable to the queue directly. Instead, it will wrap the Callable into a RunnableFuture via a call to the method AbstractExecutorService.newTaskFor(Callable), then add this RunnableFutureto the queue.