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

后端 未结 5 1856
死守一世寂寞
死守一世寂寞 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: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(workQueueSize));
    

提交回复
热议问题