[英]How to guarantee FIFO execution order in a ThreadPoolExecutor
I create a ThreadPoolExecutor with this line of code : 我用这行代码创建一个ThreadPoolExecutor:
private ExecutorService executor = new ThreadPoolExecutor(5, 10, 120, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20, true));
Then, I run 25 tasks (T01 to T25) so the situation is : 然后,我运行25个任务(T01到T25),所以情况是:
When I put 1 more task (T26), as the queue is full, I expected that the older task (T06) is removed to be launched (because MaxPoolSize is not reached) and the new task (T26) is placed at the end of the queue. 当我再添加1个任务(T26)时,当队列已满时,我预计将删除旧任务(T06)以启动(因为未达到MaxPoolSize)并且新任务(T26)被放置在末尾队列。
But in real life, if Queue is full and MaxPoolSize is not reached, the newest task is started. 但在现实生活中,如果Queue已满并且未达到MaxPoolSize,则启动最新任务。
So, I have ... 所以我有 ...
... instead of ... ... 代替 ...
Can I configure the ThreadPoolExecutor to get the expected result ? 我可以配置ThreadPoolExecutor来获得预期的结果吗? Should I use another classes ?
我应该使用其他课吗?
For information, part of ThreadPoolExecutor source code 有关信息,请参阅ThreadPoolExecutor源代码的一部分
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {
if (runState == RUNNING && workQueue.offer(command)) {
if (runState != RUNNING || poolSize == 0)
ensureQueuedTaskHandled(command);
}
else if (!addIfUnderMaximumPoolSize(command))
reject(command); // is shutdown or saturated
}
}
private boolean addIfUnderMaximumPoolSize(Runnable firstTask) {
Thread t = null;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
if (poolSize < maximumPoolSize && runState == RUNNING)
t = addThread(firstTask);
} finally {
mainLock.unlock();
}
if (t == null)
return false;
t.start();
return true;
}
Thanks 谢谢
I would make the core size equal the maximum. 我会让核心大小等于最大值。 This is how most of the pools are used and I am not sure when would be the downside in your case, but you would get the tasks executed in order.
这就是大多数池的使用方式,我不确定在你的情况下什么时候会出现缺点,但你会按顺序执行任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.