We know that the IO in Java has evolved from bio to NiO to AIO, specifically, bio is synchronous nonblocking Io,nio is synchronous non-blocking IO, and AIO is asynchronous non-blocking IO, before we analyzed the use of bio and NIO, as far as AIO is concerned, I have not yet studied the idea , first the bio and NiO study clearly, and then, this blog I summarize the previous content, if you have not read the previous blog, strongly recommend you look, here is the index link:
Summary of Java-----NiO (i.)
Java-----NIO Summary (ii)
Java-----NIO Summary (III.)
The first is synchronous/asynchronous, blocking/non-blocking differences:
Synchronous/asynchronous is the operating system level, refers to the operating system after receiving the program request IO, if the IO resource is not ready, how to respond to the problem of the program, synchronous is not responding, know that the IO resource is ready, and asynchronous will return to the program a flag, This flag is used when the IO resource is ready to be sent through the event mechanism where the content should be posted;
Blocking/non-blocking is a program level, refers to the program in the request operating system IO operation, if the IO resource is not ready, the program how to deal with the problem, blocking is the program do nothing, wait until the IO resources are ready, non-blocking the program continues to run, But occasionally to check the next IO in the end is ready?
(1): Our common socket usage, it's accept and read will cause our program blocking, for the client, after making the request, will wait until the server returns results or network anomalies, for the server side, The request event for the client can only be processed one at a time, although client A and client B requests are sent concurrently, but the execution on the server is executed sequentially and can be used to reflect this process:
(2): Subsequently, in order to resolve the service in processing client A's request can not respond to the problem of client B request, we created a thread for each client, so that each client specific execution is performed in a separate thread, As long as the main thread in the seizure of a client connection to create a thread to handle the connection related to the content on it, the main thread or continue to listen to whether or not the client access on it, this process can be reflected by:
(3): However, this approach does not solve the blocking phenomenon in a single client thread, there is still a waiting phenomenon in a single thread, and creating one for each client is more stressful for the server, and in many cases is not necessary, Java in order to solve the need to create multiple threads this problem , the introduction of NIO, he does not need to create a thread for each client on the server, but just to create a monitoring thread for selector, we can use selector to monitor those already ready channels, the data inside the buffer buffer, Or the data in the buffer buffer is written to the channel, and only selector may be stuck in a blocking state, and the operation of the channel and buffer will not fall into a blocking state; his concrete implementation schematic is:
Java-----NIO Summary (iv)