In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "detailed introduction of Java NIO", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "detailed introduction of Java NIO" bar!
First, we need to figure out several concepts: synchronous and asynchronous, blocking and non-blocking.
Synchronous and asynchronous
1. Synchronization
When a process triggers an IO operation, it must be handled by itself
For example, you have to go to the bank to withdraw money in person.
two。 Async
When the process triggers the IO operation, it does not have to handle it in person. It delegates the operation to OS. When it is delegated, it needs to tell the address and size of the data, and then does something else on its own. When the IO operation ends, it will be notified.
For example, if you give me your bank card and let me go to the bank to withdraw money for you, you need to tell me the bank card password and how much money to withdraw, and I will give you the money after I have finished withdrawing it.
3. Summary
Do it yourself is synchronous, others do it asynchronously.
Blocking and non-blocking
1. Blockage
When the process triggers the IO operation, if there is no way to read or write at this time, the process waits until the read and write is finished
For example, if you go to the bank ATM to withdraw money, and there is a queue in front of you, you have to wait until you have finished withdrawing the money.
two。 Non-blocking
When the process triggers the IO operation, if you can't read or write at this time, do something else first, wait for the notification, and then continue reading and writing.
For example, if you go to the bank counter to withdraw money, there are more people, so get a number first and wait for the number to be called before you go to the corresponding window to handle business; what is slightly inappropriate here is that while we are waiting, we still have to listen to the call number.
3. Summary
I have to wait that I can't do anything else is blocking, and I don't have to wait to do anything else is asynchronous.
BIO
Synchronous blocking; when a request comes, the application opens a thread, and when the IO is ready, the IO operation is done on its own.
The server using the BIO model is monitored by an independent Acceptor thread, and the accept () method is called in the while (true) loop to wait for the client's request.
Once the request is received, a socket can be established to start the read and write operation, and no other requests will be received until the read and write is complete
In order to enable BIO to process multiple requests at the same time, it is necessary to use multithreading; when the server receives the request, a thread is created for the client to process it, and then the thread is destroyed after the processing is completed.
However, because a request is about to start a thread, the overhead is relatively high, starting and destroying threads are expensive, and each thread takes up memory, so thread pool can be introduced to reduce the overhead of thread creation and destruction to some extent; this is also called pseudo-asynchronous IO.
The thread pool maintains N threads and a message queue; when there is a request for access, the server passes Socket as a parameter to a thread task for processing; by controlling the maximum number of threads in the thread pool and the size of the message queue, even if the number of visits is higher than the server's carrying capacity, it will not lead to downtime because the server's resources are exhausted
The efficiency of this model is good when the number of requests is not high, and there is no need to consider the problem of current restriction (controlling the maximum number of threads in the thread pool).
NIO
Synchronization is non-blocking; you don't have to wait for IO to be ready, but you will be notified when you are ready, but you still have to do the IO operation by yourself. NIO is a multiplexing mechanism that uses single-thread polling events and Channel to decide what to do to avoid performance problems caused by frequent thread switching when there are too many connections (Select phase blocking).
Hearing this, many people may have been confused. What is multiplexing? What is Channel? What exactly is the Select stage? Let me explain this in vernacular.
NIO is buffer-oriented and can read data into a buffer and process it later. NIO has several core concepts:
1. Channel and Buffer
Channel can be understood as a two-way stream, or as a channel, and Buffer is the cache, or you can think of it as a piece of memory space, and data can flow from Channel to Buffer or from Buffer to Channel.
There are many implementations of Channel, such as: FileChannel reads and writes data from files, SocketChannel reads and writes data in the network through TCP, and so on.
Buffer also has multiple types, such as ByteBuffer, CharBuffer, IntBuffer, and so on. Just looking at their names shows that they represent different data types.
2. Selector
We can think of Selector as an administrator who can manage multiple Channel, and Selector can know which Channel is ready to read and write. Such a thread only needs to operate with the administrator, which means that a thread can manage multiple Channel;. Once it listens to a prepared Channel, it can deal with it accordingly.
But Java's native NIO didn't work well until the advent of Netty.
AIO
Asynchronous non-blocking; because things are not done by themselves, in fact, there is no blocking (all non-blocking)
AIO introduces the concept of asynchronous channel on the basis of NIO; NIO uses polling to constantly ask whether the data is ready and deal with it when it is ready; AIO registers IO snooping with the operating system, and after the operating system completes the IO operation, it actively notifies and triggers the response function (do not do it yourself and let the operating system do it).
At present, what AIO should have is not very widespread.
Thank you for your reading, the above is the content of "detailed introduction of Java NIO", after the study of this article, I believe you have a deeper understanding of the detailed introduction of Java NIO, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.