Executors.newFixedThreadPool()
создает новый ThreadPoolExecutor
, используя LinkedBlockingQueue
.
С Executors.newFixedThreadPool()
:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
Когда задачи отправляются на выполнение, ThreadPoolExecutor
добавляет их в эту очередь. Кроме того, рабочие потоки будут брать задачи из этой очереди и выполнять их.
С ThreadPoolExecutor.getTask()
:
private Runnable getTask() {
// ...
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
Согласно BlockingQueue.take()
контракту, извлечение элемента из очереди также подразумевает его удаление.
/**
* Retrieves and removes the head of this queue, waiting if necessary
* until an element becomes available.
*
* @return the head of this queue
* @throws InterruptedException if interrupted while waiting
*/
E take() throws InterruptedException;