ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

后端 未结 6 1639
误落风尘
误落风尘 2020-12-12 20:25

I am working on a multithreaded project in which I need to spawn multiple threads to measure the end to end performance of my client code, as I\'m doing Load and Performance

6条回答
  •  时光取名叫无心
    2020-12-12 21:12

    In first example, You have created just 20 threads with below statement

    ExecutorService executor = Executors.newFixedThreadPool(20);
    

    In second example, you have set the thread limits range in between 20 to 2000

     ThreadPoolExecutor tpExecutor = new ThreadPoolExecutor(20, 2000, 0L, 
                                         TimeUnit.MILLISECONDS,threadPool);
    

    More threads are available for processing. But you have configured task queue as unbounded queue.

    ThreadPoolExecutor would be more effective if you have customized many or all of below parameters.

    ThreadPoolExecutor(int corePoolSize, 
                   int maximumPoolSize, 
                   long keepAliveTime, 
                   TimeUnit unit, 
                   BlockingQueue workQueue, 
                   ThreadFactory threadFactory,
                   RejectedExecutionHandler handler)
    

    RejectedExecutionHandler would be useful when you set max capacity for workQueue and number of tasks, which have been submitted to Executor are more than workQueue capacity.

    Have a look at Rejected tasks section in ThreadPoolExecutor for more details.

提交回复
热议问题