ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

后端 未结 6 1648
误落风尘
误落风尘 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:05

    After 2 days of GC out of memory exception, ThreadPoolExecutor saved my life. :)

    As Balaji said,

    [..] one more advantage is with RejectionHandler.

    In my case I had a lot of RejectedExecutionException and specifying (as follow) the discard policy solved all my problems.

    private ThreadPoolExecutor executor = new ThreadPoolExecutor(1, cpus, 1, TimeUnit.SECONDS, new SynchronousQueue(), new ThreadPoolExecutor.DiscardPolicy());
    

    But be careful! It works only if you don't need to execute all the threads that you submit to the executor.

    For further information about ThreadPoolExecutor take a look at Darren's answer

提交回复
热议问题