Java NIO 备忘

$.backlog:
可以把它简单理解为一个 listening socket 的请求连接队列的限制。当队列中的连接请求达到队列限制时,kernel 会拒绝信的连接请求。只有当应用程序通过 accept 方法从队列中取出连接请求,使队列腾出空位时,队列才能继续加入新的连接请求。对于客户端进程,如果其发出的请求被拒绝,则会抛出拒绝连接的异常。

$.SelectableChannel.register:
This method will then synchronize on the selector's key set and therefore may block if invoked concurrently with another registration or selection operation involving the same selector.
另外,就算在 register 前调用 wakeup 这么做也不是百分百保证能够唤醒,如果 wakeup 后当前进程挂起,又切换到了 select,register 还是会被挂起,不过几率比较低。有一个解决方法是 select 加一个超时时间,即 select(timeout),但这不是最优的。
参考:http://stackoverflow.com/questions/1057224/thread-is-stuck-while-registering-channel-with-selector-in-java-nio-server

原文地址:https://www.cnblogs.com/codingthings/p/4376056.html