Pitfall - Too many threads makes an application slower.

suggest change

A lot of people who are new to multi-threading think that using threads automatically make an application go faster. In fact, it is a lot more complicated than that. But one thing that we can state with certainty is that for any computer there is a limit on the number of threads that can be run at the same time:

This tells us that simply creating more and more Java threads cannot make the application go faster and faster. But there are other considerations as well:

Depending on the details of your application, these factors generally mean that there is a “sweet spot” for the number of threads. Beyond that, adding more threads gives minimal performance improvement, and can make performance worse.

If your application create for each new task, then an unexpected increase in the workload (e.g. a high request rate) can lead to catastrophic behavior.

A better way to deal with this is to use bounded thread pool whose size you can control (statically or dynamically). When there is too much work to do, the application needs to queue the requests. If you use an ExecutorService, it will take care of the thread pool management and task queuing.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents