gpt4 book ai didi

java - 在阻塞 ServerSocketChannel 上使用选择器

转载 作者:行者123 更新时间:2023-11-29 07:28:52 27 4
gpt4 key购买 nike

选择器阻塞 ServerSocketChannel 的用例是什么时候?通常,选择器如何处理阻塞 channel ?

最佳答案

这是来自 SelectableChannel javadoc :

A channel must be placed into non-blocking mode before being registered with a selector, and may not be returned to blocking mode until it has been deregistered.

请参阅 register() 的 javadoc:https://docs.oracle.com/javase/7/docs/api/java/nio/channels/SelectableChannel.html#register(java.nio.channels.Selector,%20int)

public final SelectionKey register(Selector sel, int ops)

throw :

    IllegalBlockingModeException - If this channel is in blocking mode

这是 register() 的样子:

public final SelectionKey register(Selector sel, int ops,
Object att)
throws ClosedChannelException
{
if (!isOpen())
throw new ClosedChannelException();
if ((ops & ~validOps()) != 0)
throw new IllegalArgumentException();
synchronized (regLock) {
if (blocking)
throw new IllegalBlockingModeException();
...

关于java - 在阻塞 ServerSocketChannel 上使用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com