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

标签 java nio

选择器阻塞 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/

相关文章:

java - 为什么 protected 方法不被Spring AOP拦截

java - 快速写入 : Memory-Mapped file versus BufferedWriter

multithreading - 使用 Netty 在 UDP 服务器中丢失了大量 UDP 请求

java - 在 Firebase 和 Retrieve 中保存数据

java - 二次方程因子计算

java - Selector.select(超时) x Selector.selectNow()

java - 如何在 Java NIO 中配置 socks 代理

java - 当我的java进程重命名源文件时,任何进程都可以读取目标文件吗

java - for 语句中的 final 关键字

java - include指令和<jsp :include> attribute name problem