In the network programming often uses the thread pool and the connection pool, today the common thread pool's basic application scenario and the model makes a simple contrast analysis.
1. Business process comparison
A, Non-threading pool business Flow model:
Identifies the basic thread model of a non-thread pool, the number of front-end 1 connections then the front-end client 2 and the front-end server-side 3 need to establish a one-to-one thread to respond to the connection. Front-end server-side 3 and back-end server-side 4 also need to establish a response number of threads for connection processing related business.
When a task is finished processing, the thread exits, and the front end server creates a new thread to handle the new task when the next task arrives.
b, thread pool model:
Identifies the basic thread pool model. Front-end Client a large number of connections through the service side of the task receive thread put the connection task into the front-end server-side task queue, the front-end server has a fixed number of processing threads processing the front-end task, when the processing thread finishes processing the task from the task queue to get the next processing task. Ensure that the front-end server-side and back-end server connections do not exceed the number of processing task threads on the front-end server ( n), thus guaranteeing the back-end server pressure.
When the processing thread finishes a task and the task queue does not have a task, the thread does not exit, blocking the wait for the new task.
As can be seen, the current end of the server side by setting a reasonable number of processing threads and task queue size, can effectively shield the front-end client high concurrency of the impact on the backend server side.
2. Application Scenario analysis and comparison
A, Non-thread pool model
It is suitable for a case where a single connection task takes a long time and the concurrency is not high. Once the concurrency is high, the overhead of frequent thread creation is huge.
B, thread pool model
Applicable to a single task execution time is short, but high concurrent traffic situation. There is little difference between a non-thread pool model when the number of processing threads is set to a maximum
3. Optimization Details
a, in the online pool model, you can pre-create a partial processing thread, and then create a new thread based on the actual business requirements until you create the maximum number of threads set
b. Close some idle threads for a certain period of time, reclaim some resources
Comparison and analysis of application scenarios and models of thread pool and non-thread pool