简体   繁体   English

在Java NIO中,是一个对客户端SocketChannel有用的选择器吗?

[英]In Java NIO, is a selector useful for a client SocketChannel?

In Java NIO, it is easily understandable why a ServerSocketChannel must have a selector. 在Java NIO中,很容易理解为什么ServerSocketChannel必须有一个选择器。 The selector can check from among several client channels which is ready for I/O operations. 选择器可以从几个准备好进行I / O操作的客户端通道中进行检查。

However, in some commentary I have read on the web, the selector mechanism is applied to the client SocketChannel. 但是,在我在网上阅读的一些评论中,选择器机制应用于客户端SocketChannel。 I don't understand why a selector is of use to a client. 我不明白为什么选择器对客户端有用。 Can anyone explain why it is of use in the usual circumstance where there is only one server? 任何人都可以解释为什么它只在一个服务器的通常情况下使用?

Unless you're connecting to hundreds of servers, it is difficult to see the point of non-blocking NIO in a client at all. 除非您连接到数百台服务器,否则很难在客户端看到非阻塞NIO的意义。 But if you're using non-blocking NIO, you definitely have to use a Selector , otherwise you can't know when to read the channel, or when it becomes writable again after an incomplete write. 但是,如果您使用非阻塞NIO,则必须使用Selector ,否则您无法知道何时读取通道,或者在写入不完整后再次可写。

Selectors let you service concurrent communication across multiple channels using a single thread. 选择器允许您使用单个线程跨多个通道提供并发通信。 It may be useful on a client when you must communicate with several servers concurrently, or when you communicate with peer computers in the role of a client, such as when reading a torrent. 当您必须同时与多个服务器通信时,或者在与客户端角色的对等计算机通信时,例如在读取torrent时,它可能对客户端有用。

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

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