Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Selector mechanism of Java NIO class library?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "what is the Selector mechanism of Java NIO class library". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the Selector mechanism of Java NIO class library" can help you solve the problem.

Puzzled: why consume your own resources?

What is puzzling is why our Java's New Imax O is designed to look like this. If the old Channel O cannot be multiplexed, as shown in the following figure, you have to open more than N threads to listen on each Channel (file descriptor) one by one, if this is resource-intensive and inefficient. So why do you still need to connect to yourself in the new Ithumb O mechanism, and why do you repeat the connection, consuming twice as many resources?

The WEB search engine did not find out why. I can only see that more than N people are applying for BUG, but SUN doesn't have any explanation.

The following figure shows the difference in network programming between the old IO and the new IO. It looks like NIO is really good and powerful. However, it seems that this implementation of Java will have some unnecessary overhead compared to CCompact +.

The Stone of the Mountain: learn about Selector from Apache's Mina framework

The above survey did not take long, and it happened that a colleague of Zhao Kun, a classmate, was also developing a network program, who used Apache's Mina framework. When we read the source code of the Mina framework. It is found that there is such a mechanism in Mina:

1) the Mina framework creates a thread for the Work object.

2) the run () method of the thread of the Work object takes a pile of Channel from a queue, and then uses the Selector.select () method to listen for data to read / write.

3) most importantly, during select, if a new Channel is added to the queue, Selector.select () will be awakened and then re-select*** the Channel collection.

4) to wake up the select method, simply call the wakeup () method of Selector.

For programmers familiar with system calls, there are three ways a thread blocking on the select can be awakened:

1) there is data to read / write, or an exception occurs.

2) blocking time is up, that is, time out.

3) receive a signal from non-block. Can be sent by kill or pthread_kill.

So, to wake up a blocked select, Selector.wakeup () can only use these three methods, where:

1) the second method can be excluded, because once select is blocked, it should not be possible to modify its time out time.

2) the third one seems to be implemented only on Linux, and there is no such signal notification mechanism on Windows.

So, it seems that there is only one way. Recall why each Selector.open () establishes a pair of TCP connections between itself and its own loopback in Windows; a pair of pipe is opened on Linux (pipe is usually opened in pairs under Linux), and we can guess-- that is, if you want to wake up select, you only need to send some data to your own loopback connection, so you can wake up threads blocked on the select.

The truth comes out: lovely Java, it's not easy for you.

Using the strace command under Linux, we can easily prove this. See the picture below. In the figure, please note the following points:

1) 26654 is the main thread, before I output the notify the select string to make a mark without getting lost in a lot of strace log.

2) 26662 is the listening thread, that is, the thread blocked by select.

3) the two selected lines in the figure. The write of 26654 is the system call to the wakeup () method, followed by the return of the epoll_wait of 26662.

As can be seen from the picture above, this is exactly the same as our previous guess. It can be seen that the Selector of JDK and the TCP connections or pipe built by itself are used to realize the notify and wakeup functions of Selector.

These two methods completely mimic kill and pthread_kill in Linux to signal threads blocking on select. But because signalling is not a cross-platform standard (pthread_kill is not supported by all Unix/Linux), and pipe is supported by all Unix/Linux, but not by Windows, Windows uses TCP connections to do this.

With regard to Windows, I have been wondering whether the setting of Windows's firewall will cause similar programs in Java to execute abnormally. He he. If you don't know that Java's SDK has such a mechanism, who knows how many programs will spend sleepless nights causing problems, especially Java programmers.

This is the end of the introduction on "what is the Selector mechanism of the Java NIO class library". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report