首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java-nio是如何被非阻塞混淆的

java-nio是如何被非阻塞混淆的
EN

Stack Overflow用户
提问于 2014-09-02 11:46:45
回答 1查看 635关注 0票数 1

我是NIO的新手,我理解异步套接字的概念,但我对非阻塞部分感到困惑。

我使用的是java NIO选择器。我的服务器代码是

代码语言:javascript
复制
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();
        }
    }
}

现在我的问题是:

  1. 如果我们在任何操作中使用选择器注册一个通道,它总是在selector.select()上被阻塞,那么它是如何非阻塞的。
  2. 如果我们承认它使用OP_ACCEPT作为密钥,并相应地映射通道,但是同样地,在key中是可以接受的,因为它已经被接受了,所以我正在将这个通道选择器修改为OP_READ。它再次阻塞selector.select()中的read事件

*如果我错了,请纠正我的理解*

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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:您在一个操作中同时等待多个通道和多个事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25622667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档