Multi-Threading server model for blocking the IO server model

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.