Native JDK network programming BIO

Server offers IP and listening port, the client would like to operate by connecting the server is listening address initiates a connection request handshake by three times, if the connection is successfully established, the two sides can communicate through the socket.

The traditional model of development in synchronous blocking, ServerSocket responsible for binding the IP address and start listening port; the Socket responsible for initiating the connection operation. After successful connection, the two sides synchronous blocking communication through the input and output streams. 

Traditional BIO communication model: The BIO server communication model, usually by an independent Acceptor thread is responsible for monitoring client connections, after it receives the client connection request to create a new thread for each client link processing did not handle Upon completion, the response output stream is returned to the client, the thread destroyed. I.e., a typical request-response model.

The biggest problem of this model is the lack of elastic scalability, when the increase in clients concurrent access volume, the number of threads the server and the client concurrent access in a 1: 1 proportional relationship, the Java threads in is more valuable system resources, after the rapid expansion of the number of threads, system performance will be a sharp decline, as the traffic continues to increase, the system eventually die - off - the .

To improve such a model of a threaded connection, we can use the thread pools to manage these threads, to achieve . 1 one or more thread processing N model (bottom but still use the synchronization client blocking the I / O ), commonly referred to as " pseudo asynchronous I / O model " .

We know that, if CachedThreadPool thread pool (not limit the number of threads, thread unsure if this blog article provided), in fact, in addition to automatically help us manage threads (multiplexing), it looks like a 1: 1 of client: thread model number, and the use FixedThreadPool we effectively control the maximum number of threads to ensure that the control system with limited resources to achieve N: M pseudo asynchronous I / O model.

    However, because of limits the number of threads, if the read data occurs slowly (such as the amount of data, network transmission and slow), the case of a large number of concurrent access to other news, one can only wait, this is the biggest malpractice.

How to use, see module bio under the code

Guess you like

Origin www.cnblogs.com/Soy-technology/p/11114689.html