Joker
Joker

Reputation: 11150

How java-nio is Non Blocking confused

I am new to NIO i understand the concept of Asynchronous Socket but i am confused on Non Blocking part.

I am using java NIO Selector . My Code for Server is

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();
        }
    }
}

Now My Queries are:

  1. If we register a channel with selector on any operation it always get blocked on selector.select() then how it is non blocking.

  2. If we admit it uses OP_ACCEPT as key and maps channel accordingly but again In key is acceptable i am modifying this channel selector to OP_READ since it already has been accepted. Again It blocks on selector.select() for read event

*Please correct my understanding if i am wrong *

Upvotes: 1

Views: 882

Answers (1)

user207421
user207421

Reputation: 311052

If we register a channel with selector on any operation it always get blocked on selector.select() then how it is non blocking.

select() is blocking. Every operation on a non-blocking channel itself is non-blocking, i.e. read() and write().

If we admit it uses OP_ACCEPT as key and maps channel accordingly but again In key is acceptable i am modifying this channel selector to OP_READ since it already has been accepted.

Very confused. The channel whose interest-ops == OP_ACCEPT is the listening socket. The channel you accepted from the listening socket is a connected socket, and it is this socket that you put into non-blocking mode, register with OP_ACCEPT, etc.

Again It blocks on selector.select() for read event

Correct, but it doesn't block in read() or write() or accept() or finishConnect(). Using a selector is actually called multiplexed I/O: you wait for multiple channels and multiple events at the same time in a single operation.

Upvotes: 0

Related Questions