Reputation: 388
ExecutorService.newFixedThreadPool()
Is there any real time scenarios where we prefer to have a fixed set of active threads even when there is nothing to process?
Upvotes: 0
Views: 61
Reputation: 7772
In practice, having a fixed number of threads is always better than spawning a new thread every time a task has to be processed.
Threads are expensive to create and maintain, and not being able to create the number of active threads in your application, can end up actually harming the performance. Fixed thread pools reuse already created threads and this removes the thread creation overhead.
When you keep a fixed number of threads, you can predict your memory and CPU usage better, at least IMHO.
Of course, there is no recipe that fits all use cases and, before choosing what paradigm is best for your particular situation, you should do rigorous testing and measurements. Experimenting with different configurations will give you a better understanding and point you to the best solution.
Upvotes: 2