1. Bio Programming 1.1, traditional bio programming
The basic model of network programming is the C/S model, which is the communication between two processes.
The server provides IP and listening ports, the client through the connection operation to the server to listen to the address to initiate the connection request, through three handshake connection, if the connection is established successfully, the two sides can communicate through the socket.
In the traditional development of synchronous blocking model, ServerSocket is responsible for binding IP address, initiating the listening port, and the socket is responsible for initiating the connection operation. After the connection is successful, the two sides synchronize the blocking communication through the input and output streams.
Simply describe the service-side communication model of bio:
the service side of the bio communication model, usually by a separate acceptor thread, is responsible for listening to the client connection, and after it receives the client connection request, creates a new the thread is processed and the thread is destroyed by returning the reply to the client via the output stream after processing is complete. That is typical of a request one answer overnight model.
The biggest problem of the model is the lack of elastic scalability, when the client concurrent access increases, the number of threads on the server and the number of concurrent access to the client is 1:1 proportional relationship , Java threads are also relatively valuable system resources, the number of threads rapidly expanding, the performance of the system will drop sharply, As the number of visits continues to increase, the system eventually dies----out.
Bio Example:
As you can see, the main problem with bio is that whenever a new client requests access, the server must create a new thread to handle the link, which is impossible to apply when it needs to meet high-performance, high-concurrency scenarios, because a large number of new threads can seriously affect the performance of the servers or even strike.
1.2. Using thread pool to improve bio programming
To improve this model of a connection thread, we can manage these threads using a thread pool:
we know that if you use the Cachedthreadpool thread pool, without limiting the number of threads, in fact, in addition to automatically help us manage the reuse of threads, it looks like a 1:1 request: The number of threads model, and the use of Fixedthreadpool limits the maximum number of threads, guarantees the control of system resources, and realizes the pseudo-asynchronous I/O model of n:m. However, the number of threads is limited, and if a large number of concurrent requests occur that exceed the maximum number of threads, the request waits until there are idle threads in the thread pool that can be reused. When the input stream of the socket is read, it is blocked.
So when reading data is slow, such as large data volume, network transmission slow, and so on, a lot of concurrent situations, other access to the message, can only wait, this is the biggest drawback. And NIO can solve this problem.
2. NIO Programming
The java.nio.* package in JDK 1.4 introduces a new Java I/O library, which is designed to increase speed. In fact, "old" I/O packages have been re-implemented using NIO, which can benefit even if we don't explicitly use NIO programming. The increase in speed can occur in both file I/O and network I/O, but this article discusses only the latter.
NIO provides two different socket channel implementations for Socketchannel and Serversocketchannel that correspond to sockets and serversocket in the traditional bio model.
2.1. Buffer buffers
Buffer is an object that contains some data to be written or read.
In the NIO library, all data is processed in a buffer. When the data is read, it is read directly into the buffer, and is written to the buffer when the data is written. Accessing the data in NiO at any time is through
Buffer to operate. The buffer is actually an array and provides information about the data structure access and the maintenance of the read-write location.
Specific buffers have these: Bytebuffe, Charbuffer, Shortbuffer, Intbuffer, Longbuffer, Floatbuffer, DoubleBuffer. They implement the same interface: Buffer.
2.2. Channel Channels
We read and write to the data through the channel, it is like a water pipe, is a channel. The channel differs from the flow where the channel is bidirectional and can be used for read, write, and simultaneous reading and writing operations.
The channels of the underlying operating system are generally full-duplex, so full-duplex channel streams can better map the underlying operating system's APIs.
There are two major types of channel:
1.SelectableChannel: User network read/write
2.FileChannel: For file operation
The Serversocketchannel and Socketchannel that follow the code are the subclasses of Selectablechannel.
2.4. Multiplexer Selector
Selector is the basis for Java NIO programming.
Selector provides the ability to select a task that is already in place: Selector continuously polls the channel on which it is registered, and if a read or write event occurs above a channel, the channel is in a ready state and will be
Selector polling, and then through Selectionkey can get a set of ready channel, for subsequent I/O operations.
A selector can poll multiple channel at the same time, because the JDK uses Epoll () instead of the traditional select implementation, so there is no limit of the maximum connection handle 1024/2048. so, only one thread is responsible for selector
Polling, you can access thousands of clients.
NIO Example:
Note content transferred from "http://blog.csdn.net/anxpp/article/details/51512200"
Java IO Summary (BIO, NIO, AIO)