Java:ExecutorService在一定队列大小后阻止提交 [英] Java: ExecutorService that blocks on submission after a certain queue size

查看:1356
本文介绍了Java:ExecutorService在一定队列大小后阻止提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个解决方案,其中单个线程生成可以并行执行的I / O密集型任务。每个任务都有重要的内存数据。所以我想能够限制暂时挂起的任务数量。

I am trying to code a solution in which a single thread produces I/O-intensive tasks that can be performed in parallel. Each task have significant in-memory data. So I want to be able limit the number of tasks that are pending at a moment.

如果我像这样创建ThreadPoolExecutor:

If I create ThreadPoolExecutor like this:

    ThreadPoolExecutor executor = new ThreadPoolExecutor(numWorkerThreads, numWorkerThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(maxQueue));

然后 executor.submit(callable)当队列填满并且所有线程都已经忙时,抛出 RejectedExecutionException

Then the executor.submit(callable) throws RejectedExecutionException when the queue fills up and all the threads are already busy.

我可以做什么来使 executor.submit(callable)阻塞当队列已满,忙?

What can I do to make executor.submit(callable) block when the queue is full and all threads are busy?

EDIT
我试过:

EDIT: I tried this:

executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

它有点实现了我想达到的效果,但是是一种不友好的方式。

And it somewhat achieves the effect that I want achieved but in an inelegant way.

编辑:(提出问题5年后)

(5 years after asking the question)

任何阅读此问题及其答案的人,请不要把接受的答案作为一个正确的解决方案。请阅读所有答案和评论。

To anyone reading this question and its answers, please don't take the accepted answer as one correct solution. Please read through all answers and comments.

推荐答案

我做了同样的事情。诀窍是创建一个BlockingQueue,其中的offer()方法实际上是一个put()。 (你可以使用任何你想要的BlockingQueue)。

I have done this same thing. The trick is to create a BlockingQueue where the offer() method is really a put(). (you can use whatever base BlockingQueue impl you want).

public class LimitedQueue<E> extends LinkedBlockingQueue<E> 
{
    public LimitedQueue(int maxSize)
    {
        super(maxSize);
    }

    @Override
    public boolean offer(E e)
    {
        // turn offer() and add() into a blocking calls (unless interrupted)
        try {
            put(e);
            return true;
        } catch(InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        return false;
    }

}

这篇关于Java:ExecutorService在一定队列大小后阻止提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆