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