简体   繁体   English

有什么好的资源可以解释无阻塞IO的工作原理?

[英]What are some good resources explaining how non-blocking IO works under the hood?

We all have learned from trial and error that multiple blocking threads do not scale well, and that we should switch to using NIO where we see possible. 我们都从反复试验中了解到,多个阻塞线程无法很好地扩展,我们应该在可能的情况下改用NIO。 Yet, there are not as many resources explaining why non-blocking is better by giving an under-the-hood example of how it actually works. 但是,没有太多的资源通过提供一个底层实例如何实际工作来解释为什么无阻塞更好的解释。

We all have learned from trial and error that multiple blocking threads do not scale well, 我们都从反复试验中学到,多个阻塞线程无法很好地扩展,

This was true 10 years ago, however in general, using blocking IO and NIO works well enough. 10年前确实如此,但是总的来说,使用阻塞IO和NIO效果很好。 Unless you have a very large number of connections and a service which does very little, you can support up to 1000 connections on a modern server comfortably. 除非您有大量的连接和很少执行的服务,否则您可以舒适地在现代服务器上支持多达1000个连接。 Don't forget servers are faster now, have much more cores, and people expect servers to do more work. 不要忘了服务器现在速度更快,具有更多的内核,人们期望服务器做更多的工作。 ie the bottleneck is in the application not the IO. 即瓶颈在应用程序中而不是IO中。

we should switch to using NIO where we see possible. 我们应该在可能的地方切换到使用NIO。

The main benefit is reduced thread over head. 主要好处是减少了头顶螺纹。 As I mentioned this is not as high as it was when NIO was introduced more than ten years ago. 正如我所提到的,这还不及十多年前引入NIO时的高。

NIO is much harder to work with so I would suggest only using it if you really need to. NIO很难使用,因此我建议仅在确实需要时才使用它。

there are not as many resources explaining why non-blocking is better 没有太多的资源可以解释为什么非阻塞更好

The explanation is; 解释是; you use less threads, thus you have a lower overhead. 您使用较少的线程,因此开销较低。 This only matters if the work each thread does is very little. 这仅在每个线程所做的工作很少的情况下才重要。

Note: It is often assumed that NIO mean non-blocking when actually the default behaviour of all the Channels in NIO is blocking. 注意:当实际上NIO中所有通道的默认行为处于阻塞状态时,通常会假设NIO表示非阻塞。 In fact in NIO, only TCP can be configured to be non-blocking. 实际上,在NIO中,只能将TCP配置为非阻塞。 This is the exception rather than the rule. 这是例外,而不是规则。

Note2: the fastest way to handle a small number of connections is to use blocking NIO. 注意2:处理少量连接的最快方法是使用阻塞NIO。

Finally, another benefit of using NIO is reduced copying of data if you use "direct" or native buffers. 最后,如果使用“直接”或本机缓冲区,则使用NIO的另一个好处是减少了数据复制。 However, again you need to be doing bulk transfers of data, as soon as you start reading/writing the data in a byte by byte manner eg as text, this overhead swamps the gains you might have made. 但是,再次需要批量传输数据,一旦开始以字节为单位(例如,以文本形式)读取/写入数据,那么这种开销就会淹没您可能获得的收益。

by giving an under-the-hood example of how it actually works. 通过提供一个幕后实际操作示例。

Most of the under the hood differences are either, not as much as you might imagine, or handled entirely by the operating system and thus obscured from Java or even the JVM. 大多数内在差异要么不是您想像的那么大,要么是完全由操作系统处理的,因此被Java甚至JVM所掩盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM