Java IO vs NIO vs AIO vs Protocol Servlet 3.0 and the framework of NIO

nio (new io) has been available since jdk1.4, but I have never used it. I took a look today and recorded it as follows:  
    Synchronization: I personally go out to the bank to withdraw money with a bank card (when using synchronous IO, Java handles IO reading by itself) Write).  
    Asynchronous: Entrust the younger brother to take the bank card to the bank to withdraw money, and then give it to you (when using asynchronous IO, Java delegates the IO reading and writing to the OS, and the address and size of the data buffer need to be passed to the OS (bank card and password), OS needs to support asynchronous IO operation API).  
    Blocking: ATM queues to withdraw money, you can only wait (when using blocking IO, the Java call will block until the read and write is completed before returning).  
    Non-blocking: withdraw money at the counter, take a number, and then sit on the chair and do other things. The equal sign broadcast will notify you to handle it. You cannot go until the number arrives. You can keep asking the lobby manager if the line is available. If the lobby manager says yes You can't go until it arrives (when using non-blocking IO, if you can't read or write Java, the call will return immediately, and when the IO event dispatcher will notify you can read and write, continue to read and write, and keep looping until the read and write is complete).  
1. The difference between IO NIO AIO  
io, nio, and aio is similar to the difference between resin, apache, and nginx in io processing, from blocking execution (resin) where multiple threads do not interfere with each other, to polling synchronous non-blocking (apache), to asynchronous non-blocking (nginx).  
Now these three kinds of io are supported in jdk.  
1. IO (BIO) is  
synchronized and blocked. The server implementation mode is one connection and one thread. Each thread handles io and waits for the completion of io. That is, when the client has a connection request, the server needs to start a thread for processing. If Doing nothing with this connection will cause unnecessary thread overhead, which can of course be improved by the thread pool mechanism. 
Limitations of IO: IO is a stream-oriented, blocking, serial process. For each client's socket connection, IO requires a thread to process, and during this period, this thread has been occupied until the socket is closed. During this period, tcp connection, data reading, and data return are all blocked. That is to say, a lot of CPU time slices and memory resources occupied by threads are wasted during this period.  
Each time a Socket connection is established, a new thread is created to communicate with the Socket separately (communication in a blocking manner). This method has a high response speed and is very simple to control. It is very effective when the number of connections is small, but if a thread is generated for each connection, it is undoubtedly a waste of system resources. A larger number will result in insufficient resources.  
2. NIO (new IO) is synchronous and non-blocking since jdk1.4  
. The server implementation mode is one thread per request, and each thread handles io by itself, but another thread polls to check whether io is ready, without waiting for io to complete, That is, the connection requests sent by the client will be registered on the multiplexer, and the multiplexer will only start a thread for processing when there is an I/O request connected to the multiplexer.  
NIO is buffer-oriented, non-blocking, selector-based, and uses a thread to poll and monitor multiple data transmission channels. Which channel is ready (that is, there is a set of data that can be processed), it will be processed which channel.  
The server saves a Socket connection list, and then polls the list. If it finds that there is data to read on a Socket port (read ready), it calls the corresponding read operation of the socket connection; When there is data to write (ready to write), the corresponding write operation of the socket connection is called; if the socket connection of a port has been interrupted, the corresponding destructor method is called to close the port. In this way, the server resources can be fully utilized, and the efficiency has been greatly improved.  
3. AIO (Asynchronous io, NIO.2) starts from jdk1.7 
http://stevex.blog.51cto.com/4300375/1284437  
Asynchronous and non-blocking, the server implementation mode is a valid request for one thread, and the client's I/O request is completed by the OS first and then notifies the server application to start the thread. Processing, each thread does not have to handle io by itself, but delegates os to handle it, and does not need to wait for io to complete. If it is completed, os will notify.  
Using the epoll model of linux.  
3. Conclusion  
In the case of few connections, traditional IO is easy to write and easy to use. But with the increase of the number of connections, the problem of traditional IO will not work. Because traditional IO processing consumes one thread for each connection, and the efficiency of the program increases with the increase of the number of threads when the number of threads is small, but after a certain number, it decreases with the increase of the number of threads. Therefore, the bottleneck of traditional blocking IO is that it cannot handle too many connections.  
The purpose of non-blocking IO is to solve this bottleneck. How is non-blocking IO implemented? The number of threads for non-blocking IO processing connections is not related to the number of connections, which means that processing 10,000 connections for non-blocking IO does not require 10,000 threads. You can use 1,000 or 2,000 threads to handle it. Because non-blocking IO handles connections asynchronously. When a connection sends a request to the server, the server treats the connection request as a request "event" and assigns this "event" to the corresponding function for processing. We can put this handler function into the thread to execute, and return the thread after execution. This way a thread can handle multiple events asynchronously. And blocking IO threads spend most of their time waiting for requests.  
Then NIO's non-blocking requires constant polling, which is also more resource-intensive. Therefore, AIO  
4, BIO, NIO, and AIO are applicable to scenarios.  
    The BIO method is suitable for a structure with a relatively small number of connections and a fixed number of connections. This method has relatively high requirements on server resources, and concurrency is limited to applications. The only choice before JDK1.4, but The program is intuitive and easy to understand. 
    The NIO method is suitable for architectures with a large number of connections and relatively short connections (light operation), such as chat servers, where concurrency is limited to applications, and programming is more complicated. JDK1.4 began to support it.  
    The AIO method is used for architectures with a large number of connections and relatively long connections (heavy operations), such as photo album servers. It fully calls the OS to participate in concurrent operations, and the programming is more complicated. JDK7 began to support it.  
ps: AIO and NIO are based on IO and are not meant to replace IO.  
2. The concept of IO in the framework of NIO  
:  
1. As far as IO is concerned: there are 5 models in concept: blocking I/O, nonblocking I/O, I/O multiplexing (select and poll), signal driven I/O (SIGIO ), asynchronous I/O (the POSIX aio_functions).  
2. Then, different operating systems support the above models differently: Unix supports io multiplexing, and different systems have different names: kqueue in freebsd; epoll in linux. And windows: IOCP was born in 2000 to support the last asynchronous I/O  
3.java is a cross-platform language. In order to support asynchronous IO, nio was born. NIO 1.0 introduced by Java1.4 is based on I/O complex use. Different reuse methods will be selected on each platform. Linux uses epoll, BSD uses kqueue, and Windows should be overlapping I/O (certainly not IOCP).  
4: Based on jdk's nio, different companies have produced a bunch of frameworks: apache mina, jboss's netty, and sun's grizzly. These are tcp/udp directly encapsulating the transport layer. 
It is difficult to use nio directly, so there are encapsulations for the network io part (tcp/udp-transport layer) such as netty (nio also has non-network io parts), in order to make nio easier to use. Netty, etc. is just a nio framework and does not require additional support for web containers, which means that web containers are not limited.  
The three NIO frameworks are:  
1. Mina  
Mina (Multipurpose Infrastructure for Network Applications) is a relatively new project organized by Apache, which provides a very convenient framework for developing high-performance and high-availability network applications. The currently released Mina version 2.04 supports the development of TCP/UDP application programs and serial communication programs based on JavaNIO technology, and the functions supported by Mina are also being further expanded. At present, the applications that are using Mina include: Apache Directory Project, AsyncWeb, AMQP (Advanced MessageQueuing Protocol), RED5 Server (Macromedia? FlashMedia RTMP), ObjectRADIUS, Openfire and so on.
2. Netty  
Netty is an asynchronous event-driven network application framework and tool for the rapid development of maintainable high-performance, highly scalable protocol servers and clients. That said, Netty is a NIO client/server framework that enables fast and easy development of network applications such as protocol servers and clients. It greatly simplifies network programming, such as TCP and UDP socket servers.  
3. Grizzly  
Grizzly is an application framework designed to solve various problems that arise when writing thousands of users accessing the server. Use Javanio as a base and hide the complexity of its programming. Easy-to-use high-performance API. Brings non-blocking socketd to the protocol processing layer. Take advantage of high-performance buffering and buffer management using a high-performance thread pool. 
3. Servlet3.0 vs NIO  
servlet3.0 is a specification or protocol, which can be implemented by IO or NIO, while NIO is just a technical implementation. Similar to one is the architecture, the other is the specific technology.  
Same:  
both provide asynchronous functionality.  
Difference:  
JDK's nio is more difficult to use directly, so there are netty packages for the network io part (tcp/udp-transport layer) (nio also has non-network io parts), in order to make nio easier to use.  
servlet3.0 is another thing, not the encapsulation of io, but one of the many specifications of javaee6. Any implementation of javaee6 (or the implementation of a web container like tomcat) will support servlet 3.0. In theory, servlet can support a variety of application layer protocols (not just http), and the asynchronous features provided after servlet 3.0 are the same as The nio or aio provided by javase is not directly related, that is, the asynchronous features provided in servlet 3.0 can be realized by using bio.  
It can be said that the asynchrony of NIO is the lower-level IO that is directly processed, while the asynchrony of Servlet 3.0 refers to the asynchrony of the upper-layer request response 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327055883&siteId=291194637