On BIO, NIO, AIO introduction and application of scene analysis technology

Personal blog navigation page (click on the right link to open a personal blog): Daniel take you on technology stack 

IO way is usually divided into several, simultaneous obstruction of BIO, synchronous non-blocking NIO, asynchronous non-blocking AIO.

A synchronous blocking BIO

Before JDK1.4, we establish a network connection using BIO mode, you need to start a serverSocket on the server, and then start the client socket to communicate to the server, the server establishes a need for each request by default heap thread waits for the request, and the client sends a request, first consult if there is a thread corresponding server, if it will not have to wait or rejected the request, if any, the client thread waits for the end of the request before continuing execution.

Second, the synchronous non-blocking NIO

NIO itself is based on event-driven ideas to complete, it mainly want to solve the big problem of concurrent BIO, the use of synchronous I / O, network applications, if you want to simultaneously handle multiple client requests, or to the client At the same time, and multiple servers to communicate, you must use multiple threads to handle. That is, each client request is assigned to a thread to handle separately. While doing so can meet our requirements, but also brought another problem. Since each create a thread is assigned to a memory space for this thread, and the operating system itself has certain limitations on the total number of threads. If the client requests too much, because the server program may be overwhelmed and rejected the client's request, the server may thus even paralysis.

NIO based Reactor, when the socket has a socket stream read or write, the operating system notifies the corresponding reference program for processing, and then applied to the stream buffer to read or write operation system. That is, this time, is not a connection must correspond to a processing thread, but rather valid request, corresponds to a thread, when no data connection is not working threads to handle.

BIO and NIO a more important difference is that we use BIO, it tends to introduce multithreading, each connected to a separate thread; and NIO is single-threaded or use a small amount of threads, each connected to a common thread.

NIO most important place when a connection is created, need not correspond to a thread, the connection will be registered on the multiplexer, so all connections requires only one thread can get, when the multiple thread when the multiplexer to poll found that requests, a thread to process before opening the connection, i.e. a request for a thread mode.

In the treatment of NIO, when a request to the open thread for processing, may wait for back-end application resources (JDBC connections, etc.), in fact, this thread is blocked, if concurrent up, there will be the same as BIO The problem.

After the HTTP / 1.1 appeared, with Http long connection, so in the NIO process may further evolution of this in addition to indicate a specific time-out and shut the http header, this link has been opened state can be achieved in back-end resources resource pool or queue, when a request, then, open thread the request and the request data to the backend resource pool or queue inside returns and maintain this site (which request which connection, etc.) in the global areas, so that the front still thread to accept other requests, the backend processing application only need to perform queue inside it, so that the processing request is asynchronous and back-end application. when the trailing end processed, to give place to the global scene, generating a response this is realized asynchronous processing.

Third, non-blocking asynchronous AIO

NIO different and, when read and write operations, simply called directly read or write to the API method. Both methods are asynchronous, for the read operation, when a stream can be read, the operating system will read the stream into the buffer read process, and notifies the application program; for the write operation, when when the operating system will write method for delivering a stream writing has been completed, the operating system notifies the active application. That can be understood as, read / write methods are asynchronous, will take the initiative to call the callback function when finished. In JDK1.7, this part becomes AIO,

Predominantly at java.nio.channels package adds the following four asynchronous channels:

  • AsynchronousSocketChannel
  • AsynchronousServerSocketChannel
  • AsynchronousFileChannel
  • AsynchronousDatagramChannel

Wherein the read / write method, the object returns a callback function, when executing the read / write operation after directly call the callback function.  

BIO is a connecting thread.

NIO is a request thread.

AIO is a valid request a thread.

First to understand how the concept of example, a bank teller, for example: 

  • Sync: Bank card holders themselves personally to the bank to withdraw money (when using synchronous IO, Java IO deal with their own reading and writing);
  • Asynchronous: commission a younger brother to take the bank card to the bank to withdraw money, and then give you (when using asynchronous IO, Java OS will be entrusted to handle IO read and write, you need to pass data buffer address and size of the OS (bank cards and passwords), OS needs to support asynchronous IO operations API);
  • Obstruction: ATM withdrawals queue, you have to wait (when to use blocking IO, Java has been blocked calls will not return to complete the reading and writing);
  • Non-blocking: teller counter, take a number, and then sitting in a chair doing anything else, you will be notified of the equal sign broadcast deal with, not to the number you just can not go, you can always ask the manager lined up in the lobby did not, lobby manager also said that if no you can not go to (the use of non-blocking IO, if you can not read and write Java call will return immediately, when the IO event dispatcher will inform the reader can continue to read and write, read and write continuously loop until completion)

Four, java support of BIO, NIO, AIO's

java BIO: synchronous and blocking

In this way, the user initiates a process after the IO operation, we must wait for the completion of IO operations, after truly done only when the IO operation, the user process to run. JAVA traditional IO model belong to this way!

Server model to achieve a connecting thread, that client has the server side need to start a thread for processing when the connection request if the connection does not do anything to cause unnecessary overhead of thread, of course, can be improved by a thread pool mechanism.

java NIO: Non-blocking synchronization

Side in such a manner, the user initiates a process IO operation can later return to do other things, but the process needs to ask the user IO operations from time to time is ready, stop the process which requires the user to ask questions, thereby introducing unnecessary CPU resources waste. JAVA's NIO which currently belongs to synchronize non-blocking IO.

A request server implements a thread mode, i.e., the connection request sent by the client are registered to the multiplexer, the multiplexer is connected to the polling I / O start request when a thread for processing.

java AIO: asynchronous non-blocking

In this mode, the user only needs to initiate a process IO operation and then return immediately, and other IO operations after the completion of a true, the application will be notified IO operation is completed, in which case the user only needs to process the data processing just fine, It does not require the actual IO read and write operations, because the real IO read or write operation has been completed by the kernel. Currently in Java IO model also did not support this. 

Server mode to achieve a valid request a thread, client I / O requests are completed before the OS and then inform the server application to start a thread for processing.

Five, BIO, NIO, AIO applicable scene analysis

BIO method is applicable to the number of connections is relatively small and fixed infrastructure, server resources in this way to high demand, limited concurrent applications, JDK1.4 only option before the program just simple and easy to understand.

NIO method is applicable to many and relatively short number of connections architectures, such as chat server, concurrent limited to the application, the program is more complex, JDK1.4 began to support.

AIO method is applicable to more than the number of connections and the connection longer architecture, such as the album server, call the OS to fully participate in concurrent operation, programming is more complex, JDK1.7 began to support.

Six, Tomcat (BIO) and Jetty (NIO)

Tomcat and Jetty are currently two of the most famous worldwide open source webserver / servlet container.

Same point:

1, Tomcat and Jetty servlet engine is a kind, they all support the standard servlet specification and JavaEE specifications.

difference:

1, Schema Compare

(1) Jetty architecture simpler than Tomcat.

(2) Jetty's architecture is based on the handler to be achieved, mainly extensions can be achieved using the handler, simple expansion.

(. 3) Tomcat architecture is based on the design of the container, it is extended to understand the structure of the overall design Tomcat, difficult to expand.

2, performance comparison

(1) Jetty and Tomcat performance is insignificant.

(2) Jetty can handle a large number of connections and can remain connected for a long time, and so on for web chatting application.

(3) Jetty a simple structure, and therefore as a server, Jetty components can be loaded on demand, reducing unnecessary components, reducing the internal server overhead and improve server performance.

(4) Jetty defaults NIO more dominant on the end of request processing I / O, when dealing with static resources, higher performance.

3, Tomcat suitable for handling a very busy few links, higher overall performance Tomcat. Tomcat defaults BIO processing I / O requests, when dealing with static resource poor performance.

4, other more

(1) Jetty applications faster, simple modifications, to better support the new servlet specification.

(2) Tomcat currently used widely, and support for servlet javaEE more comprehensive, integrated directly come in a lot of performance.

Attached Java / C / C ++ / machine learning / Algorithms and Data Structures / front-end / Android / Python / programmer reading / single books books Daquan:

(Click on the right to open there in the dry personal blog): Technical dry Flowering
===== >> ① [Java Daniel take you on the road to advanced] << ====
===== >> ② [+ acm algorithm data structure Daniel take you on the road to advanced] << ===
===== >> ③ [database Daniel take you on the road to advanced] << == ===
===== >> ④ [Daniel Web front-end to take you on the road to advanced] << ====
===== >> ⑤ [machine learning python and Daniel take you entry to the Advanced Road] << ====
===== >> ⑥ [architect Daniel take you on the road to advanced] << =====
===== >> ⑦ [C ++ Daniel advanced to take you on the road] << ====
===== >> ⑧ [ios Daniel take you on the road to advanced] << ====
=====> > ⑨ [Web security Daniel take you on the road to advanced] << =====
===== >> ⑩ [Linux operating system and Daniel take you on the road to advanced] << = ====

There is no unearned fruits, hope you young friends, friends want to learn techniques, overcoming all obstacles in the way of the road determined to tie into technology, understand the book, and then knock on the code, understand the principle, and go practice, will It will bring you life, your job, your future a dream.

Published 47 original articles · won praise 0 · Views 265

Guess you like

Origin blog.csdn.net/weixin_41663412/article/details/104898835