What is the use of a Thread pool in Java?

前端 未结 7 1939
醉梦人生
醉梦人生 2020-12-04 09:02

What is the use of a Thread pool? Is there a good real world example?

相关标签:
7条回答
  • 2020-12-04 09:48

    Already great answers are there to explain it but Lets understand it with an example:

    Problem without thread pool: Consider a web server application where each HTTP request is handled by a separate thread. If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of all those threads exceed the capacity of the system.

    Solution with thread pool: With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as quickly as they come in, but it will be servicing them as quickly as the system can sustain.

    For more details(overhead of all the threads): Why is creating a Thread said to be expensive?

    0 讨论(0)
提交回复
热议问题