workQueue 的大小可以超过 maximumPoolSize 吗?

Can size of workQueue be more than maximumPoolSize?

我们有 ThreadPoolExecutor,它的初始化看起来像:

LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maxThreads, maxThreads, 50000L, TimeUnit.MILLISECONDS, queue);

maxThreads的值是200,但是有一段时间queue.size() > 200,为什么可以呢?

maxThreads表示池化线程的最大数量。 您的队列大小表示等待执行的排队任务的数量。

corePoolSizemaximumPoolSize 决定池的大小,BlockingQueue 的大小才是最重要的。

您使用的是 LinkedBlockingQueue,它基于链接结构,默认最大大小为 Integer.MAX_VALUE。它还允许您通过传递所需的大小来使用其构造函数更改其最大大小,如下所示:

LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(200);

通过在其构造函数中传递 200,您的池将最多容纳 200 个任务