gpt4 book ai didi

java - 是否有具有队列限制的 ExecutorService 实现以及用新队列成员替换旧队列成员的选项?

转载 作者:行者123 更新时间:2023-12-02 01:57:30 24 4
gpt4 key购买 nike

我需要一个 ExecutorService 实现来限制可以排队的 Runnable 数量。我还希望能够控制在队列已满时提交新的可运行对象时发生的情况。理想情况下,我希望能够选择一种策略,但只需删除队列前面的可运行程序并将新的可运行程序放在队列末尾就足够了。我确信肯定已经实现了类似的东西,但我环顾四周但找不到任何东西。

最佳答案

使用DiscardOldestPolicy :

A handler for rejected tasks that discards the oldest unhandled request and then retries execute, unless the executor is shut down, in which case the task is discarded.

和具有固定容量的BlockingQueue:

int fixedThreadNumber = 10;
int idleSeconds = 10;

BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(10);

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
fixedThreadNumber,
fixedThreadNumber,
idleSeconds, TimeUnit.SECONDS,
blockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy());

ExecutorService executor = threadPoolExecutor;

关于java - 是否有具有队列限制的 ExecutorService 实现以及用新队列成员替换旧队列成员的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52109822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com