Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would you want to use two cached thread pools?

Consider this example from a Netty tutorial (although it's not the only example of it I've seen):

Executor bossPool = Executors.newCachedThreadPool();
Executor workerPool = Executors.newCachedThreadPool();
ChannelFactory channelFactory = new NioClientSocketChannelFactory(bossPool, workerPool);

Two separate cached thread pools are created here. But why? I can understand the purpose of multiple threads pools if they are of fixed size, but cached thread pools are not. So why would you want to have multiple cached thread pools if you can just have one that constantly expands? In fact, wouldn't it be better to have just one? Because if you have two different threads submitting tasks, then it means it is more likely that the idle worker threads are utilized.

I'm wondering about this because I'm writing the networking for a server, and I am handling UDP and TCP connections separately. I want to dispatch handling tasks to a thread pool and I am considering using cached thread pools for that. However, I don't know if I should use one or two.

Thanks.

like image 334
Martin Tuskevicius Avatar asked Oct 21 '22 12:10

Martin Tuskevicius


1 Answers

The only reason I can think of to have 2 cached thread pools is if you need to do operations on the pools differently. For example, you might have two classes of tasks and you want to do awaitTermination() on one of the thread-pools that handles one class of tasks but not the other. Or maybe you want to shutdownNow() one of the pools without affecting the other pool that you will allow to drain. Or maybe you have a different thread factory for each of the pools -- although not evident in your code example.

like image 185
Gray Avatar answered Oct 24 '22 12:10

Gray