简体   繁体   English

Java NIO 为什么 Selector 必须在非阻塞模式下与 Channel 一起工作?

[英]Java NIO why Selector has to work with Channel in non-blocking mode?

According to this passage , IO Multiplexing can work with file descriptors in both blocking and non-blocking mode:根据这一段,IO Multiplexing 可以在阻塞和非阻塞模式下与文件描述符一起工作: 在此处输入图片说明

And non-blocking mode is only needed to cooperate with edge-triggered mode:而非阻塞模式只需要配合边沿触发模式: 在此处输入图片说明

What's more, according to this answer , Java NIO works in lever-triggered mode.更重要的是,根据这个答案,Java NIO 在杠杆触发模式下工作。

Then why Java NIO Selector requires the channel to be in the non-blocking mode:那么为什么 Java NIO Selector 要求通道处于非阻塞模式: 在此处输入图片说明

Thanks to your reply in advance.感谢您提前回复。

The power of java.nio and the Channel comes from the fact that you can watch over multiple sockets in a non-blocking way. java.nioChannel的强大之处在于您可以以非阻塞方式监视多个套接字。 In other words, the traditional java.io was a single-thread / single-socket approach with blocking IO calls, whereas java.nio is meant for a usage where you can spawn thousands of socket connection (via channels and selector) without requiring thousands of threads.换句话说,传统的java.io是一种具有阻塞 IO 调用single-thread / single-socket方法,而java.nio的用途是在不需要数千个套接字连接的情况下生成数千个套接字连接(通过通道和选择器)线程。 Here the job of the selector is to help your thread / thread pool read & write from the channels which are ready and connected.这里选择器的工作是帮助你的线程/线程池从准备好和连接的通道中读取和写入。

Java Docs for Channel defines it as,频道的 Java 文档将其定义为,

Defines channels, which represent connections to entities that are capable of performing I/O operations, such as files and sockets;定义通道,表示与能够执行 I/O 操作的实体的连接,例如文件和套接字; defines selectors, for multiplexed, non-blocking I/O operations定义选择器,用于多路复用、非阻塞 I/O 操作

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

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