For the characteristics of the single-threaded server model, we can improve it so that it can respond to multiple clients simultaneously. The simplest improvement is the use of multi-threaded (or multi-process) server models, at the application layer level, where we typically use multithreaded mode. Multithreading enables multiple clients to request at the same time, and can respond to these requests almost at the same time, without having to queue up a single processing to provide a single answer to multiple clients at the same time.
Figure 2-6-1-2 multithreaded blocking server model
The core of a multithreaded server model is the use of multithreaded mechanisms to assign a thread to each client. 2-6-1-2, the server Initializes a ServerSocket instance, binds a port number, and listens to the client for access. Two clients come to the request almost at the same time, after the server receives the client request, it creates two threads to process until the response is complete. In this process, the server is simply responsible for receiving messages, which are then handed to the processor thread to handle, and the server can receive requests from multiple clients in a short period of time.
This model is also blocking because each thread writes a message to the client that is not returned immediately, and the time slice of the black block in Figure 2-6-1-2 is blocking time. However, this mode is significantly higher than single-threaded processing, and does not have to wait until the first request is processed to process the second one. For multi-threading, if the number of threads is greater than the number of CPUs, then the operating system will be dispatched between multiple threads, so that other threads can use the CPU, each time a dispatch causes a context switch, most of the general-purpose processors, each scheduling requires about 5000-10000 clock cycles, about a few subtle.
Can this multi-threaded server model really solve the problem perfectly? In fact, as the number of clients more and more, when the hundreds of thousands of connection requests, multi-threaded operation will seriously consume the system CPU resources, and multithreading more easily into the suspended animation state. In this paper, the concept of "thread pool" is put forward, when the thread pool is started, a certain number of line enters upgradeable is created and put into memory, and it is put back into the pool for use by the new task instead of actually destroying the thread. This buffer pooling mechanism can effectively reduce the frequency of thread creation and destruction, and greatly reduce the system overhead. This mechanism is also reflected in Tomcat, 2-6-1-3, when a request comes over to try to fetch a thread from the pool, if there are idle threads to return a thread for use, and if there is no idle thread, then directly shut down the socket denial of service, to ensure that the server is operating within the scope of processing power
Figure 2-6-1-3 Multithreaded blocking server model-thread pool
Multi-threaded Server model features: can support the response to multiple clients at the same time, processing capacity is greatly improved, there is a large amount of concurrency, but the server system resource consumption is large, but also has a more complex structure, the implementation of the code is relatively more complex. This model is applicable to the large number of accesses, the request is short-term, stateless, the business logic is more complex, the response time is more demanding occasions.