简体   繁体   English

java Selector是异步或非阻塞体系结构

[英]java Selector is asynchronous or non-blocking architecture

For me below are the most probable definitions for asynchronous and non-blocking I/O: 对我来说,下面是异步和非阻塞I / O最可能的定义:

Asynchronous I/O: In asynchronous I/O applications return immediately and OS will let them know when the bytes are available for processing. Asynchronous I/O:在异步I / O应用程序中立即返回,操作系统会在字节可用于处理时让他们知道。

NON-blocking I/O: Here application returns immediately what ever data available and application should have polling mechanism to find out when more data is ready. NON-blocking I/O:此处应用程序立即返回可用的数据,应用程序应具有轮询机制以找出何时准备好更多数据。

After knowing these definitions if we analyze java channels ie SocketChannel , ServerSocketChannel , DatagramSocketChannel then we can find that these channels can be used as blocking or non-blocking mode by the method configureBlocking(boolean block) . 在了解了这些定义之后,如果我们分析java通道,即SocketChannelServerSocketChannelDatagramSocketChannel那么我们可以通过configureBlocking(boolean block)方法找到这些通道可以用作阻塞或非阻塞模式。 and assume that we are using them as non-blocking mode. 并假设我们将它们用作非阻塞模式。 So here comes the questions: 所以这里有问题:

if i will use Selector ie register channels to a selector whether it is Asynchronous I/O or NON-blocking I/O ? 如果我将使用Selector即将通道注册到selector无论是异步I / O还是非阻塞I / O

I feel this is Asynchronous I/O in java if and only if underlying operating system is informing the java application about readiness selection of a channel. 我觉得这是java中的异步I / O,当且仅当底层操作系统通知java应用程序有关通道的准备选择时。 Else it is non-blocking I/O and selector is just a mechanism which helps us polling the above mention channels as i mention in the definition. 否则它是非阻塞I / O和selector只是一种机制,它帮助我们轮询上面提到的通道,正如我在定义中提到的那样。 Which is correct? 哪个是对的? Thanks in advance. 提前致谢。

EDIT: 编辑:

I have answered one part of the question ie types of I/O and how java facilitates these functionality. 我已经回答了问题的一部分,即I / O的类型以及java如何促进这些功能。

But one question still remains whether all these functionalities are provides by java is simulated at java layer or it uses underlaying OS to facilitate? 但仍有一个问题仍然是java是否提供所有这些功能是在java层模拟还是使用底层操作系统来促进? Assume the underlaying OS has all the support for these functionalities. 假设底层操作系统具有对这些功能的所有支持。

Please refer to the answer. 请参考答案。

I thought of answering my question by doing some more homework. 我想通过做更多的功课回答我的问题。 This post will also help in understanding the I/O concepts wrt underlying OS. 这篇文章还将有助于理解底层操作系统的I / O概念。

  • This is blocking I/O: FileInputStream , FileOutputStream and even reading to or writing from Socket come under this category 这是阻塞I / O: FileInputStreamFileOutputStream甚至从Socket读取或写入属于此类别

  • This is non-blocking I/O: this is used by Socket Channels like ServerSocketchannel , SocketChannel , DatagramChannel in Java 这是非阻塞I / O:这是由Java中的ServerSocketchannelSocketChannelDatagramChannel等套接字通道使用的

  • This is multiplexed I/O: in Java it is used by Selector to handle multiple channels and these channels should be non-blocking by nature. 这是多路复用的I / O:在Java中, Selector使用它来处理多个通道,这些通道本质上应该是non-blocking的。 So Socket channels can be registered to Selector and Selector can manage by I/O multiplexing facility of underlaying OS. 因此Socket通道可以注册到SelectorSelector可以通过OS下载的I / O多路复用工具进行管理。

  • Now comes Asynchronous I/O. 现在是异步I / O. In asynchronous I/O applications return immediately and OS will let them know when the bytes are available for processing. 在异步I / O应用程序中立即返回,OS将在字节可用于处理时让他们知道。 In java it is facilitated by AsynchronousSocketChannel , AsynchronousServerSocketChannel , AsynchronousFileChannel . 在java中, AsynchronousSocketChannelAsynchronousServerSocketChannelAsynchronousFileChannel提供了便利。

For these above functionalities java uses underlying OS heavily. 对于以上这些功能,Java大量使用底层操作系统。 This is evident when i was going through the book . 当我阅读这本书时,这是显而易见的。 Here in chapter 4 the author mentions that 在第4章中,作者提到了这一点

True readiness selection must be done by the operating system. 真正的准备选择必须由操作系统完成。 One of the most important functions performed by an operating system is to handle I/O requests and notify processes when their data is ready. 操作系统执行的最重要功能之一是处理I / O请求并在数据准备就绪时通知进程。 So it only makes sense to delegate this function down to the operating system. 因此,将此功能委托给操作系统才有意义。 The Selector class provides the abstraction by which Java code can request readiness selection service from the underlying operating system in a portable way. Selector类提供抽象,Java代码可以通过该抽象以可移植的方式从底层操作系统请求准备选择服务。

Hence it's clear that Java uses underlying OS heavily for these features. 因此很明显,Java会对这些功能大量使用底层操作系统。

if i will use Selector ie register channels to a selector whether it is Asynchronous I/O or NON-blocking I/O? 如果我将使用Selector,即将通道注册到选择器,无论是异步I / O还是非阻塞I / O?

The channels are doing non-blocking I/O. 通道正在进行非阻塞I / O. The Selector itself is doing multiplexed I/O. 选择器本身正在执行多路复用 I / O. Asynchronous I/O in Java is done via Futures, and in other languages via semaphores or callbacks. Java中的异步I / O通过Futures完成,其他语言通过信号量或回调完成。

But one question still remains whether all these functionalities are provides by java is simulated at java layer or it uses underlaying OS to facilitate? 但仍有一个问题仍然是java是否提供所有这些功能是在java层模拟还是使用底层操作系统来促进? Assume the underlaying OS has all the support for these functionalities. 假设底层操作系统具有对这些功能的所有支持。

The operating system does it. 操作系统就是这样做的。 Applications can't, and Java qualifies as an application to the operating system. 应用程序不能,并且Java有资格作为操作系统的应用程序。

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

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