Java, NIO, AIO-2JavaGive me a chestnut.
Then, then, the C/S mode, the reactor mode, and the Proactor mode are the common processing models for server processing IO, and this article explains these modes:
Take a food and beverage as an example, each person to eat is an event, he will first look at the menu, then order food. Just like a website will have a lot of requests, ask the server to do something. We need our service staff to deal with these dining events.
The way in which multithreading is handled is this:
A person comes to dinner, a waiter goes to the service, then the guest will look at the menu and order. The waiter will give the menu to the back chef.
Two people to eat, two waiter to service ...
Five people to eat, five waiter to service ...
This is the multithreading approach, an event arrives, there will be a thread service. Obviously this way in the case of less people will have a very good user experience, each guest feel that they are VIP, dedicated service. If the restaurant has been in this way for up to 5 guests at the same time, this restaurant can be very well served. (This is very familiar, this is the simplest to have a socket connection, create a thread.) )
But more and more people are satisfied with this restaurant, the source is more, at the same time to eat people to 20 people, the boss is not happy, and then please waiter, occupy the place do not say, also to open wages, and then people will save money. What do we do? The boss thought, 10 waiter to deal with 20 guests is also able to deal with, waiter diligent point is good, wait for a guest to wait for another, or time. Considering the overall consideration, the boss decided to use a thread pool of 10 service personnel. (Create a fixed number of thread pools, build a task queue, and use multithreading to process tasks)
But if the number of people too much, then 10 person's waiter is not busy, similarly, no one, 10 people in idle waiting for the waiter and caused a lot of waste of resources.
The boss later found that the guests order relatively slow, the big waiter is waiting for the guests to order, its hard work is not too much. Boss can be the boss of course a bit different place, finally found a new method, that is: when the guests order, the waiter can go to greet other guests, and so the guests ordered a good dish, directly greeted a "waiter", immediately there is a waiter past service. The boss decided to change his business model. (this is reactor mode!!) )
After the boss made a reputation, rich, and sprouted a new idea, do takeout. The user calls to tell the receipt of his orders, and then the order to send orders to the food and beverage department, and then the single-member can continue to work orders, and other food and beverage departments to do the order, tell the order to complete to pick up the meal, the order information to take the meal to the takeout brother sent out. (This is Proactor mode!!) )
The preceding example illustrates these patterns, which may be so confusing that the following is a detailed explanation of each of these patterns:
1. c/s programming model in Bio
The basic model of network programming is the C/S model, which is a communication between two processes. The server provides IP and listening ports, the client initiates connection requests to the port on the servers, and through three handshake if the connection succeeds, the two sides can make socket communication, which is the TCP connection, of course, just nonsense.
In the traditional synchronous blocking development model, the server ServerSocket is responsible for binding the IP address, initiates the listening port, the socket is responsible for initiating the connection, and after the connection is successful, the two sides synchronously block communication through the input and output stream. On the server side of the Bio communication model, there is usually a separate thread that listens to the client's connection, which receives the client's request connection and creates a thread for each client that connects to the link, destroying the thread after the connection ends. See Example:
Bio Communication model
The biggest disadvantage of the model is the lack of elastic scalability, the number of service threads and the number of concurrent access to the client is 1:1 of the relationship, when the number of clients too many times, the server may not be able to withstand, frequent creation, destruction of threads also consumes system resources.
2. Camouflage Asynchronous Programming Model
A one-to-one TCP connection service is a waste of resources, and in order to improve this, you can use the thread pool to manage these threads and why it is pseudo-async, because there is no change in the thread-pool threads as well as on-the-go services. The use of thread pool knowledge can effectively control and constrain resources.
Pseudo-Asynchronous IO programming model
When there are a limited number of threads in the pool, if there are a large number of concurrent requests, you can wait until the maximum number of threads is used, knowing that there are idle threads in the thread pool that can be used, and that the threading model of the socket is handled in a blocking manner.
3.Reactor mode
Reactor mode
The reactor mode is used for synchronizing IO. The developer starts by registering the time of interest with the event splitter and providing the appropriate processing function. The time separator waits for an event to be applied or the state of an operation changes (for example, the file descriptor can read or write, or the socket can read or write), and the event splitter passes the event to the time-registered event handler or callback function, which completes the actual read and write operation. The following is a read operation example to illustrate the specific steps of reactor:
- Program registers read-ready events and associated processors
- Event separator waits for events to occur
- When a read-ready event occurs, the event splitter invokes the event handler registered in the first step
- The event handler performs the actual read operation. If necessary, it can claim to be interested in this read-ready event again, repeating the above steps.
Think this is actually very virtual, the third will demonstrate the specific code.
4.Proactor mode
Proactor mode
The Proactor mode is used for asynchronous IO. It is very similar to the reactor pattern, and it is necessary to register the event to the time splitter and provide the corresponding processing function. However, this event is not a ready event, but instead initiates an asynchronous read-write operation (request) event, but the actual work is done by the operating system. When initiating an event, the required parameters include a buffer for storing the data, and a callback function after the request is completed. The event splitter waits for the event to complete, then forwards the completed event to the corresponding handler function or callback, etc.
The following is an example of a read event to illustrate the specific steps of the pattern:
- The application initializes an asynchronous read operation and then registers the appropriate event handler, at which point the event handler does not focus on the read-ready event, but instead focuses on the read completion event, which is the key to distinguish it from the reactor.
- Event Splitter waits for read operation completion event
- When the event splitter waits for the read operation to complete, the operating system calls the kernel thread to complete the read operation and puts the contents of the read into the buffer that the user passes over. This is also a bit different from reactor, where the application needs to pass the buffer proactor.
- After the event separator captures the read completion event, activates the event handler registered by the application and the event handler reads the data directly from the buffer without the need for actual read operations. If necessary, you can continue registering the event to the event handler, repeating the above steps.
Briefly summarize the similarities and differences between reactor and Proactor:
- Their same local framework is roughly the same, the program needs to register the corresponding time to the event separator, the event separator according to the state changes to inform the corresponding event handler. Are all event-driven IO communication multiplexing models.
- They are different places, concerned about the event is not the same, reactor mode is concerned about the readiness of the State of time, Proactor mode is concerned about the completion status of the event. The reactor mode program reads or writes the data itself, while in Proactor mode, the application does not need to perform the actual read/write process, it only needs to read or write from the buffer, The operating system reads the buffer or writes the buffer to the real IO device (Proactor requires an API interface for the operating system to provide sophisticated asynchronous operations).
So in general, synchronous and asynchronous is relative to the application and the core of the interaction, synchronization needs to proactively ask, and asynchronous when the kernel at the time of the Io event to notify the application, and blocking and non-blocking is only the system calls the system when the function of the implementation of the way.
Resources
- 8458299 Reactor and Proactor modes
- HTTP://DAIMOJINGDEYU.ITEYE.COM/BLOG/828696 reactor mode, or reactor mode
- http://xmuzyq.iteye.com/blog/783218 reactor and Proactor modes for high performance IO design
- https://www.cnblogs.com/charlesblc/p/6072827.html High Performance IO design & Java NIO & Synchronous/Asynchronous blocking/non-blocking Reactor/proactor
- https://www.cnblogs.com/charlesblc/p/6072827.html NiO Learning
- http://www.cnblogs.com/fanzhidongyzby/p/4098546.html High Performance IO Model analysis (recommended)
- 51512200#T3 Java Network IO Programming Summary (BIO, NIO, AIO all with full instance code) (recommended)
- 51503329 Linux Network I/O model introduction (recommended)
Java, NIO, AIO-2