【问题标题】:Is there an ExecutorService implementation with queue limit and an option to replace old queue members with new ones?是否存在具有队列限制的 ExecutorService 实现以及用新队列成员替换旧队列成员的选项?
【发布时间】:2018-08-31 06:40:12
【问题描述】:

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

【问题讨论】:

    标签: java multithreading runnable executorservice blockingqueue


    【解决方案1】:

    使用DiscardOldestPolicy

    拒绝任务的处理程序,丢弃最旧的未处理任务 请求然后重试执行,除非执行器被关闭,在 在这种情况下任务被丢弃。

    和固定容量的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;
    

    【讨论】:

      猜你喜欢
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多