我是NIO的新手,我理解异步套接字的概念,但我对非阻塞部分感到困惑。
我使用的是java NIO选择器。我的服务器代码是
public class EcoNonBlockingIOServer_7 {
public static int PORT_NUMBER = 5555;
public static void main(String[] argv) throws Exception {
new EcoNonBlockingIOServer_7().go(argv);
}
public void go(String[] argv) throws Exception {
int port = PORT_NUMBER;
if (argv.length > 0) { // Override default listen port
port = Integer.parseInt(argv[0]);
}
System.out.println("Listening on port " + port);
// Allocate an unbound server socket channel
ServerSocketChannel serverChannel = ServerSocketChannel.open();
// Get the associated ServerSocket to bind it with
ServerSocket serverSocket = serverChannel.socket();
// Create a new Selector for use below
Selector selector = Selector.open();
// Set the port the server channel will listen to
serverSocket.bind(new InetSocketAddress(port));
// Set nonblocking mode for the listening socket
serverChannel.configureBlocking(false);
// Register the ServerSocketChannel with the Selector
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
// This may block for a long time. Upon returning, the
// selected set contains keys of the ready channels.
int n = selector.select();
if (n == 0) {
continue; // nothing to do
}
// Get an iterator over the set of selected keys
Iterator it = selector.selectedKeys().iterator();
// Look at each key in the selected set
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
// Is a new connection coming in?
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel channel = server.accept();
registerChannel(selector, channel, SelectionKey.OP_READ);
sayHello(channel);
}
// Is there data to read on this channel?
if (key.isReadable()) {
readDataFromSocket(key);
}
// Remove key from selected set; it's been handled
it.remove();
}
}
}现在我的问题是:
*如果我错了,请纠正我的理解*
发布于 2014-09-02 12:27:15
如果我们在任何操作中使用选择器注册一个通道,它总是在selector.select()上被阻塞,那么它是如何非阻塞的。
select()阻塞了。在非阻塞信道上的每一个操作本身都是非阻塞的,即read()和write().。
如果我们承认它使用OP_ACCEPT作为密钥,并相应地映射通道,但是同样地,在key中是可以接受的,因为它已经被接受了,所以我正在将这个通道选择器修改为OP_READ。
很困惑。其兴趣-操作== OP_ACCEPT是侦听套接字的频道。您从侦听套接字接受的通道是一个连接的套接字,您将这个套接字放入非阻塞模式,并向OP_ACCEPT注册等等。
它再次阻塞selector.select()中的read事件
正确,但它不会阻塞在read()或write()或accept()或finishConnect()中。使用选择器实际上被称为多路复用I/O:您在一个操作中同时等待多个通道和多个事件。
https://stackoverflow.com/questions/25622667
复制相似问题