In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "problem case Analysis of java nio socket". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The first question:
During the development of java nio, when we have a channel object, the first step is to configure its blocking mode to be non-blocking. ServerSocketChannel.configureBlocking (false)
Let's try to understand what this configureBlocking function does.
But in the end, we found a function of native, and it doesn't seem easy to understand exactly what's going on inside.
So let's try what happens if we configure it as true.
As a result, when register went to selector, it was reported wrong directly.
Java.nio.channels.IllegalBlockingModeException
Although we haven't got a definite answer to this question, it seems that it can basically solve part of our confusion. To use selector, it can only be false.
Second, there is a simple example of java nio, in which we use three values of SelectionKey, OP_READ, OP_CONNECT, and OP_ACCEPT. But if we look at the java source code, we will find that there is actually another value, called OP_WRITE, how this value is used, because in our previous example, the data exchange between server and client has been basically completed. After receiving the read event, write the response back through channel. It seems that there is nothing wrong with OP_WRITE!
First of all, let's reinterpret the values of OP_READ, OP_CONNECT and OP_ACCEPT. OP_READ, for example, should be interpreted as read ready. In other words, the other party has already written data to me and started to read it at this time.
So OP_WRITE is easy to explain, write ready, so what are the conditions for writing ready? In general, there can be two conditions, their own write buffer is free, the network environment is unblocked.
Let's try what happens if we register an OP_WRITE of SocketChannel with selector.
Modify the previous code, the server side, after receiving a client connection to call this sentence
Channel.register (this.selector, SelectionKey.OP_WRITE); / / originally OP_READ
In this case, because the network is unblocked and the local write buffer has plenty of space, you will always try to do what you write.
To sum up, when the network environment is very good, and the amount of tasks written is not very large, the use of OP_WRITE may not be needed, and in some cases of high concurrency, OP_WRITE may still have its practical use.
This is the end of the content of "problem case Analysis of java nio socket". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.