I am using the Executors
framework in Java to create thread pools for a multi-threaded application, and I have a question related to performance.
I have an application which can work in realtime or non-realtime mode. In case it's realtime, I'm simply using the following:
THREAD_POOL = Executors.newCachedThreadPool();
But in case it's not realtime, I want the ability to control the size of my thread pool. To do this, I'm thinking about 2 options, but I don't really understand the difference, and which one would perform better.
Option 1 is to use the simple way:
THREAD_POOL = Executors.newFixedThreadPool(threadPoolSize);
Option 2 is to create my own ThreadPoolExecutor
like this:
RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (Exception e) {}
}
};
THREAD_POOL = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10000), rejectHandler);
I would like to understand what is the advantage of using the more complex option 2, and also if I should use another data structure than LinkedBlockingQueue
? Any help would be appreciated.