ExecutorService, standard way to avoid to task queue getting too full

后端 未结 5 1857
死守一世寂寞
死守一世寂寞 2021-01-31 16:36

I am using ExecutorService for ease of concurrent multithreaded program. Take following code:

while(xxx) {
    ExecutorService exService = Executors         


        
相关标签:
5条回答
  • 2021-01-31 16:40

    The trick is to use a fixed queue size and:

    new ThreadPoolExecutor.CallerRunsPolicy()
    

    I also recommend using Guava's ListeningExecutorService. Here is an example consumer/producer queues.

    private ListeningExecutorService producerExecutorService = MoreExecutors.listeningDecorator(newFixedThreadPoolWithQueueSize(5, 20));
    private ListeningExecutorService consumerExecutorService = MoreExecutors.listeningDecorator(newFixedThreadPoolWithQueueSize(5, 20));
    
    private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      5000L, TimeUnit.MILLISECONDS,
                                      new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy());
    }
    

    Anything better and you might want to consider a MQ like RabbitMQ or ActiveMQ as they have QoS technology.

    0 讨论(0)
  • 2021-01-31 16:45

    You can call ThreadPoolExecutor.getQueue().size() to find out the size of the waiting queue. You can take an action if the queue is too long. I suggest running the task in the current thread if the queue is too long to slow down the producer (if that is appropriate).

    0 讨论(0)
  • 2021-01-31 16:51

    you can add another bloquing queue that's has limited size to controle the size of internal queue in executorService, some thinks like semaphore but very easy. before executor you put() and whene the task achive take(). take() must be inside the task code

    0 讨论(0)
  • 2021-01-31 16:53

    A true blocking ThreadPoolExecutor has been on the wishlist of many, there's even a JDC bug opened on it. I'm facing the same problem, and came across this: http://today.java.net/pub/a/today/2008/10/23/creating-a-notifying-blocking-thread-pool-executor.html

    It's an implementation of a BlockingThreadPoolExecutor, implemented using a RejectionPolicy that uses offer to add the task to the queue, waiting for the queue to have room. It looks good.

    0 讨论(0)
  • 2021-01-31 16:56

    You're better off creating the ThreadPoolExecutor yourself (which is what Executors.newXXX() does anyway).

    In the constructor, you can pass in a BlockingQueue for the Executor to use as its task queue. If you pass in a size constrained BlockingQueue (like LinkedBlockingQueue), it should achieve the effect you want.

    ExecutorService exService = new ThreadPoolExecutor(NUMBER_THREADS, NUMBER_THREADS, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(workQueueSize));
    
    0 讨论(0)
提交回复
热议问题