簡體   English   中英

Java-nio如何使Non Blocking感到困惑

[英]How java-nio is Non Blocking confused

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

我正在使用Java NIO Selector。 我的服務器代碼是

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作為鍵並相應地映射通道,但是同樣可以接受In鍵,因為該通道選擇器已經被接受,因此我正在將該通道選擇器修改為OP_READ。 再次,它在selector.select()上阻止讀取事件

*如果我錯了,請更正我的理解*

如果我們在任何操作上向選擇器注冊一個通道,則該通道總是在selector.select()上被阻塞,那么它是如何非阻塞的。

select()正在阻止。 非阻塞通道本身的每個操作都是非阻塞的,即read()write().

如果我們承認它使用OP_ACCEPT作為鍵並相應地映射通道,但是同樣可以接受In鍵,因為該通道選擇器已經被接受,因此我正在將該通道選擇器修改為OP_READ。

很迷茫。 興趣操作== OP_ACCEPT的通道是偵聽套接字。 偵聽套接字接受的通道是已連接的套接字,並且您已將此套接字置於非阻塞模式,向OP_ACCEPT注冊等。

再次,它在selector.select()上阻止讀取事件

正確,但是它不會阻塞read()write()accept()finishConnect() 使用選擇器實際上稱為多路復用 I / O:您在一次操作中同時等待多個通道和多個事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM