简体   繁体   English

使用NIO和NIO.2的Java非阻塞和异步IO(JSR203) - Reactor / Proactor实现

[英]Java Non-Blocking and Asynchronous IO with NIO & NIO.2 (JSR203) - Reactor/Proactor Implementations

So here I am reading one of my favorite software pattern books (Pattern-Oriented Software Architecture - Patterns for Concurrent and Networked Objects), specifically the sections on Proactor/Reactor asynchronous IO patterns. 所以我在这里阅读我最喜欢的软件模式书之一(面向模式的软件架构 - 并发和网络对象的模式),特别是有关Proactor / Reactor异步IO模式的部分。 I can see how by using selectable channels I can implement a Reactor style asynchronous IO mechanism quite easy (and have done so). 我可以看到如何通过使用可选择的通道,我可以很容易地实现Reactor风格的异步IO机制(并且已经这样做了)。 But, I cannot see how I would implement a proper Proactor mechanism with non-blocking writes. 但是,我无法看到如何使用非阻塞写入实现适当的Proactor机制。 That is taking advantage of OS managed non-blocking write functions. 这是利用OS管理的非阻塞写入功能。

Functionality supported by OS specific calls like GetQueuedCompletionStatus under win32. 操作系统特定调用支持的功能,如win32下的GetQueuedCompletionStatus

I did see that Java 7 brings some updates to NIO with asynchronous completion handlers (which seems to be in the right direction). 我确实看到Java 7通过异步完成处理程序为NIO带来了一些更新(这似乎是在正确的方向)。 That being said... Given the lack of unified cross-platform support for OS managed async operations (specifically async write) I am assuming that this is a quassy-implementation that is not utilizing native OS support. 话虽如此......鉴于缺乏对操作系统管理的异步操作(特别是异步写入)的统一跨平台支持,我假设这是一个不使用本机操作系统支持的quassy实现。

So my questions are, is proactor based IO handling possible in Java in such a way that it is advantageous to use for specific scenarios; 所以我的问题是,基于proactor的IO处理是否可以在Java中以这样的方式使用,以便用于特定场景; and, if Java NIO does support proactor based IO handling (either in Java 6 or Java 7) is OS managed asynchronous IO support (ie completion callbacks from the OS) being utilized? 并且,如果Java NIO确实支持基于proactor的IO处理(在Java 6或Java 7中),那么是否正在使用OS管理的异步IO支持(即来自OS的完成回调)? Furthermore, if the implementation is purely in-VM are the performance benefits so little that using proactive event handling offers nothing more than a different (possibly simpler) way of constructing concurrent network handling software. 此外,如果实现纯粹是在VM中,则性能优势很小,使用主动事件处理只提供构建并发网络处理软件的不同(可能更简单)方式。

For anyone interested in proactive event handling here is a good article that outlines pros / cons and a comparison to both traditional thread-per-connection and reactive IO models. 对于那些对主动事件处理感兴趣的人来说, 这是一篇很好的文章 ,概述了优点/缺点以及与传统的每个连接线程和反应式IO模型的比较。

There are lots of factors involved in this one. 这个涉及很多因素。 I will try to summarize my findings as best as possible (aware of the fact that there is contention regarding the usefulness of reactor and proactor IO handling implementations). 我将尽可能地总结我的发现(意识到关于反应堆和反应器IO处理实施的有用性存在争论)。

Is proactor based IO handling possible in Java in such a way that it is advantageous to use for specific scenarios. 基于proactor的IO处理在Java中是否可行,以便用于特定场景是有利的。

Java 1.4 introduced non-blocking IO which is NOT the same as asynchronous IO. Java 1.4引入了非阻塞IO,它与异步IO不同。 Java SE 7 introduces asynchronous IO with JSR203 making "true" proactor style IO handling implementations possible. Java SE 7引入了JSR203的异步IO,使“真正的”proactor样式IO处理实现成为可能。

See AsyncrhonousSocketChannel , AsynchronousServerSocketChannel 请参见AsyncrhonousSocketChannelAsynchronousServerSocketChannel

and, if Java NIO does support proactor based IO handling (either in Java 6 or Java 7) is OS managed asynchronous IO support (ie completion callbacks from the OS) being utilized? 并且,如果Java NIO确实支持基于proactor的IO处理(在Java 6或Java 7中),那么是否正在使用OS管理的异步IO支持(即来自OS的完成回调)?

Reading through the JSR 203 specs, completion handlers using new asynchronous channels are definitely supported and it is reported that native OS features are being utilized but I have not ascertained to what extent yet. 阅读JSR 203规范,肯定支持使用新异步通道的完成处理程序,并且据报道正在使用本机OS功能,但我还没有确定到什么程度。 I may follow up on this after an analysis of the Java 7 source (unless someone beats me to it). 在对Java 7源代码进行分析之后,我可能会对此进行跟进(除非有人打败我)。

Furthermore, if the implementation is purely in-VM are the performance benefits so little that using proactive event handling offers nothing more than a different (possibly simpler) way of constructing concurrent network handling software. 此外,如果实现纯粹是在VM中,则性能优势很小,使用主动事件处理只提供构建并发网络处理软件的不同(可能更简单)方式。

I have not been able to find any performance comparisons regarding new Asynchronous IO features in Java 7. I'm sure they will become available in the near future. 我无法找到有关Java 7中新的异步IO功能的任何性能比较。我相信它们将在不久的将来上市。

As always, when presented with more than one way to tackle a problem the questions of which approach is better is almost always answered with "depends". 与往常一样,当提出解决问题的方法不止一种时,哪种方法更好的问题几乎总是以“依赖”来回答。 Proactive event handling (using asynchronous completion handlers) is included with Java 7 and cannot simply exist without purpose. Java 7中包含主动事件处理(使用异步完成处理程序),并且不能无目的地存在。 For certain applications, it will make sense to use such IO handling. 对于某些应用程序,使用此类IO处理是有意义的。 Historically a common example given where proactor has good applicability is in a HTTP server where many short requests are issued frequently. 历史上,在proactor具有良好适用性的情况下给出的一个常见示例是在HTTP服务器中,其中经常发出许多短请求。 For a deeper explanation give this a read (provided only to highlight the advantages of proactor so try to overlook the fact that example code is C++). 为了更深入的解释, 给出一个读取 (仅提供突出显示proactor的优点,因此试图忽略示例代码是C ++的事实)。

IMO it seems obvious that in many circumstances reactor/proactor complicate what would otherwise be a very simple design using a more traditional approach and in other more complex systems they offer a high degree of simplification and flexibility. IMO似乎很明显,在许多情况下,反应器/反应器使使用更传统方法的非常简单的设计复杂化,而在其他更复杂的系统中,它们提供高度简化和灵活性。

. . .

On a side note I highly recommend reading through the following presentation about NIO which offers performance comparison between NIO and the "traditional" approach. 另外,我强烈建议您阅读以下有关NIO的演示文稿,演示文稿提供了NIO与“传统”方法之间的性能比较。 Though I would also advise caution regarding the results presented as the NIO implementation in the benchmark was based on the pre Java 1.4 NBIO NIO library and not the NIO implementation shipped in 1.4. 虽然我也建议谨慎对待所提供的结果,因为基准测试中的NIO实现基于Java 1.4 NBIO NIO库而不是1.4中提供的NIO实现。

I would check you really need to worry about blocking writes. 我会检查你真的需要担心阻止写入。

A read blocks where there is no data to read. 读取块,其中没有要读取的数据。 This can be most of the time. 这可能是大部分时间。 However, a write blocks when the buffers are full, this happens very rarely and often indiciates a slow connection or a failed consumer. 但是,当缓冲区已满时写入会阻塞,这种情况很少发生,并且通常表示连接速度较慢或消费者失败。

If you want non-blocking IO, do it for the reads, and therefor for the writes as well. 如果您想要非阻塞IO,请执行读取操作,以及写入操作。

Note: Using blocking IO with NIO is usually simpler and can out perform non-blocking NIO unless you have 1000s of connections, you are likely to find the complexity added is not worth it. 注意:对NIO使用阻塞IO通常更简单并且可以执行非阻塞NIO,除非您有1000个连接,您可能会发现添加的复杂性是不值得的。 (And is possibly not the best option) (可能不是最好的选择)

NIO already provides an implementation of the reactive pattern (selectors), and NIO2 adds an implementation of the proactive pattern (completion handlers). NIO已经提供了反应模式 (选择器) 的实现,NIO2添加了主动模式的实现 (完成处理程序)。

Don't reinvent it, just use it, because you cannot beat its performance - which is what anyone trying to avoid blocking i/o is after after all - with a pure Java solution, as you don't get access to the non-blocking / asynchronous features of the underlying OS. 不要重新发明它,只是使用它,因为你无法击败它的性能 - 这是任何试图避免阻塞i / o的人毕竟 - 使用纯Java解决方案,因为你无法访问非底层操作系统的阻塞/异步功能。 But NIO and NIO2 make use of those, which makes them fast. 但是NIO和NIO2使用它们,这使它们变得快速。

one of my favorite software pattern books (Pattern-Oriented Software Architecture - Patterns for Concurrent and Networked Objects) 我最喜欢的软件模式书籍之一(面向模式的软件架构 - 并发和联网对象的模式)

With respect that book is very out of date and of dubious relevance at any date. 尊重这本书非常过时,并且在任何时候都具有可疑的相关性。 It came out of the design pattern frenzy of the late 1990s when there was a concerted attempt to reduce the whole of computer science to design patterns. 它源于20世纪90年代后期的设计模式狂热,当时人们齐心协力将整个计算机科学简化为设计模式。

My present view is that NIO is already a framework and a design pattern. 我现在的观点是,NIO已经是一个框架和设计模式。

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

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