Compared to custom thread pool, source which refers to Oracle documentation recommends using common thread pool when parallel streaming.
Since the common pool has same number of threads with the number of CPU cores what's the difference between using a common thread pool and custom thread pool which consists of same number of threads with the common pool?
CodePudding user response:
what's the difference between using a common thread pool and custom thread pool which consists of same number of threads with the common pool?
Some important considerations:
With the common thread pool, you don't need to write or maintain or configure or manage your own thread pool.
The common thread pool exists and is used, and it already provides as many threads as there are cores. No more threads than that can make progress at the same time anyway, so if you set up a custom pool and it and the common one are both fully subscribed for non-blocking operations then you incur additional overhead from the additional threads but no speedup.
CodePudding user response:
It might be better to ask, what is the advantage of a custom thread pool? It's more work, so why do it?
Processors are a global resource. Custom thread pools can be created in different places without any coordination, and thus "over-allocate" the processors.
A re-usable library should not create custom thread pools; if you can justify why a custom thread pool is desirable, it should be managed at a higher level in the application. To support this, a re-usable library might permit a caller to configure (inject) a custom executor, and fall back to the common thread pool by default.