Java NIO – Non Blocking IO and Multiplexing

Introduction

Traditional IO is blocking in nature. For example traditional socket connection is blocking, wherein the server waits for a client connection and once the client connection is obtained an independent thread handles the operation of the client. Java NIO introduces non blocking IO wherein a thread does not have to wait for read or write events. It also introduces multiplexing, a concept that can be explained as follows : imagine a waiter in a restaurant attending at a table. If the restaurant manager had to assign one waiter per table then the restaurant would quickly go out of business. Instead the waiter attends multiple tables (multiplexing) and does not wait permanently on one table (non blocking). Before we go into the code, lets look into some basic classes that make up the ‘Selector’ framework.

Important Classes

  • NetworkChannel– is a channel to a network socket. The bind method binds the socket to a local address.
  • SelectableChannel– Defines method using what a channel can be multiplexed (used in conjunction with other channels) with the help of a Selector. Selectable channels can be used by multiple threads A selectable channel may operate in a blocking or nonblocking mode.
  • Selector – Selector handles the combination of multiple selectablechannels. A selectablechannel is registered to a selector. This registration is represented by a selectionKey. A selector mentions the registered keys, selected keys and cancelled keys. The operating system is checked for the readiness of each channel. As soon as a channel is ready its key is put into the selected keys. A selector may be used by concurrent threads by their selection keys may not.
  • AbstactSelectableChannel-Represents the basic implementation of selectable channels. It contains methods for registering, deregistering and closing channels. It also has the information of whether the channel is blocked or not and keeps a list of current keys
  • SocketChannel-It represents a channel for a socket, such that the channel can be selected. Socket channels support non blocking connections. Socket Channels are thread safe

We now look at an example of the above classes in action. This is a server socket that listens for incoming client connections.

The client that connects to the server.

3 thoughts on “Java NIO – Non Blocking IO and Multiplexing

  • October 27, 2016 at 8:13 am
    Permalink

    I have a question about the ServerSocketChannelExample class. We have only one thread so how the server can read and write at the same time? I think it is still blocking. For example if there is 2 client socket channel one of which is ready for write and the other is ready for read. Then main thread can not read from readable channel before it writes to writable channel. If it is so what is the advantage of channel an selector?

    Reply
    • October 31, 2016 at 8:21 am
      Permalink

      I will update the tutorial some time this week to answer this question.

      Reply
      • May 23, 2017 at 5:42 am
        Permalink

        Can you please answer the above question.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.