In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java BIO,NIO,AIO is what", in daily operation, I believe many people in Java BIO,NIO,AIO is what the problem is there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Java BIO,NIO,AIO is what" doubts helpful! Next, please follow the small series to learn together!
There are several terminology concepts that often confuse us in high-performance IO architecture design. The details are as follows:
1 What is synchronization?
2 What is asynchronous?
3 What is blocking?
4 What is non-blocking?
5 What is synchronous blocking?
6 What is synchronous non-blocking?
7 What is asynchronous blocking?
8 What is asynchronous non-blocking?
Let's start with an example from life:
If you want to eat chicken:
Synchroblock: You go to a restaurant and order, and then you wait while yelling,"Ready?"
Synchronous non-blocking: ordering dinner at a restaurant and walking the dog. However, after a while, he went back to the restaurant and shouted,"Is it ready?"
Asynchronous blocking: When walking the dog, I received a phone call from a restaurant saying that the meal was ready and asked you to get it yourself.
Asynchronous non-blocking: The restaurant calls and says, we know where you are, we'll send it to you, just walk the dog.
Before figuring out the above problems, we first have to understand what is synchronous, asynchronous, blocking, and non-blocking. Only these individual concepts are clearly understood, and then it is relatively easy to understand them in combination.
1. Synchronous and asynchronous are specific to the interaction between the application and the kernel.
2. Blocking and non-blocking are different ways for processes to access data according to the ready state of IO operations. To put it bluntly, it is an implementation mode of read or write operation functions. In blocking mode, read or write functions will always wait, while in non-blocking mode, read or write functions will immediately return a state value.
The above description can be summarized in a short sentence, synchronous and asynchronous are the purpose, blocking and non-blocking are the implementation.
1. Synchronization: refers to user processes triggering IO operations and waiting or polling to see if IO operations are ready. Go out to buy clothes yourself, do it yourself, and do nothing else.
2. Asynchronous: Asynchronous means that after the user process triggers the IO operation, it starts to do its own thing, and when the IO operation has been completed, it will get the IO completion notification (asynchronous characteristic is notification) to tell friends the size, size and color of the appropriate clothes, so that friends can entrust them to sell, and then they can do other things. (When using asynchronous IO, Java delegates IO read and write to OS, and needs to pass the address and size of the data buffer to OS)
3. Blocking: The so-called blocking mode means that when trying to read and write the file descriptor, if there is nothing to read or write at that time, the program enters a waiting state until there is something to read or write. Go to the bus station to recharge, and find that at this time, the recharge member is not there (may go to the toilet), and then we wait here until the recharge member comes back. (Of course, this is not true in real life, but it is true in computers.)
4. Non-blocking: non-blocking state, if there is nothing to read, or can not write, the read-write function immediately returns, and will not wait, when the bank withdraws money to do business, receive a receipt, after receiving it, we can play with mobile phones, or chat with others, when it is our turn, the bank speaker will notify, then we can go.
An IO operation is actually divided into two steps: initiating the IO request and the actual IO operation.
The difference between synchronous IO and asynchronous IO is whether the second step blocks. If the actual IO reads and writes block the request process, it is synchronous IO.
The difference between blocking IO and non-blocking IO is that the first step is whether the initiating IO request will be blocked. If it is blocked until completion, it is a traditional blocking IO. If it is not blocked, it is a non-blocking IO.
Synchronous and asynchronous refer to the interaction between the application and the kernel. Synchronous refers to the user process triggering the IO operation and waiting or polling to see if the IO operation is ready, while asynchronous refers to the user process triggering the IO operation and then starting to do its own thing, and when the IO operation is completed, it will be notified that the IO operation is completed.
Blocking and non-blocking are different ways for processes to access data according to the ready state of IO operations. To put it bluntly, it is a way to implement read or write operation functions. In blocking mode, read or write functions will always wait, while in non-blocking mode, read or write functions will immediately return a state value.
Therefore,IO operations can be divided into three categories: synchronous blocking (i.e., early BIO operations), synchronous non-blocking (NIO), and asynchronous non-blocking (AIO).
Synchronous Block (BIO):
In this way, after initiating an IO operation, the user process must wait for the IO operation to complete, and only when the IO operation is actually completed can the user process run. JAVA The traditional IO model falls into this category.
Synchronous Non-blocking (NIO):
In this way, the user process initiates an IO operation and then returns to do something else, but the user process needs to ask if the IO operation is ready from time to time, which requires the user process to keep asking, thus introducing unnecessary CPU waste. Java's NIO is currently non-blocking.
Asynchronous Non-blocking (AIO):
In this way, after the application initiates an IO operation, it does not wait for the completion of the kernel IO operation, and the kernel will notify the application after the IO operation is completed.
Synchronous Block IO (JAVA BIO):
Synchronization and blocking, the server implementation mode for a connection a thread, that is, the client has a connection request when the server needs to start a thread to process, if the connection does not do anything will cause unnecessary thread overhead, of course, can be improved through the thread pool mechanism.
Synchronous non-blocking IO(Java NIO):
Synchronous non-blocking, the server implementation mode is one thread per request, that is, the connection request 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 for the connection. The user process also needs to ask from time to time whether the IO operation is ready, which requires the user process to keep asking.
Asynchronous blocking IO (Java NIO):
In this way, after the application initiates an IO operation, it does not wait for the completion of the kernel IO operation. After the kernel completes the IO operation, it will notify the application program. This is actually the most critical difference between synchronous and asynchronous. Synchronous must wait or actively ask whether IO is completed. So why is it blocked? Because this is done through the select system call, and the select function itself is implemented in a blocking manner, one advantage of using the select function is that it can listen to multiple file handles at the same time (if from the UNP point of view, select is a synchronous operation). Because after select, the process also needs to read and write data), thus improving the concurrency of the system!
(Java AIO(NIO.2)) Asynchronous non-blocking IO:
In this mode, the user process only needs to initiate an IO operation and then immediately return. After the IO operation is really completed, the application program will be notified that the IO operation is completed. At this time, the user process only needs to process the data, and there is no need to carry out the actual IO read and write operation, because the real IO read or write operation has already been completed by the kernel.
BIO, NIO, AIO applicable scenario analysis:
BIO mode is suitable for a relatively small number of connections and fixed architecture. This mode requires high server resources and concurrency is limited to applications. JDK1.4 is the only choice before, but the program is intuitive and easy to understand.
NIO mode is suitable for architectures with a large number of connections and relatively short connections (light operation), such as chat servers, concurrency is limited to applications, programming is more complex, JDK 1.4 began to support.
The AIO mode is used for architectures with a large number of connections and relatively long connections (heavy operations), such as album servers, fully calling OS to participate in concurrent operations, programming is more complex, JDK7 begins to support.
At this point, the study of "BIO,NIO,AIO in Java" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.