对Accept事件的处理是注册产生的SocketChannel进入Selector监听,对输入输出的等待并不存在,所以是不是就没有区别?也就是说可以使用下面的方式进行处理,// Create a new server socket
_acceptChannel = ServerSocketChannel.open();
// Set to blocking mode
_acceptChannel.configureBlocking(true);
启动一个不断轮询的while循环,while (isRunning() && getConnection() != null)
{
try
{
accept(_acceptor);
}
catch (EofException e)
{
LOG.ignore(e);
}
catch (IOException e)
{
LOG.ignore(e);
}
catch (InterruptedException x)
{
// Connector has been stopped
LOG.ignore(x);
}
catch (Throwable e)
{
LOG.warn(e);
}
}
将接受到的链接请求派发到其他的Selector线程中取就可以了,这样Accepter线程根本不用绑定Selector了/* ------------------------------------------------------------ */
@Override
public void accept(int acceptorID) throws IOException
{
ServerSocketChannel server;
synchronized(this)
{
server = _acceptChannel;
}
if (server!=null && server.isOpen() && _manager.isStarted())
{
SocketChannel channel = server.accept();
channel.configureBlocking(false);
Socket socket = channel.socket();
configure(socket);
_manager.register(channel);
}
}