Java NIO

Four main IO models

  1. Blocking IO
  2. Non-blocking IO (NIO)
  3. IO Multiplexing -> JAVA NIO (New IO)
  4. Asynchronous IO

Three main components of Java NIO

  1. Channel
  2. Buffer
  3. Selector


Eight kinds of buffer,including ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBUffer, LongBuffer, ShortBuffer, and MappedByBuffer.

    // Invariants: mark <= position <= limit <= capacity
    private int mark = -1;
    private int position = 0;
    private int limit;
    private int capacity;


  1. allocate()
  2. put()
  3. flip()
  4. get()
  5. rewind()
  6. mark()
  7. reset()
  8. clear()


Four main channels, including FileChannel, SocketChannel, ServerSocketChannel, and DatagramChannel.

  1. FileChannel: file channel
  2. SocketChannel: TCP socket
  3. ServerSocketChannel: Listen TCP socket
  4. DatagramChannel: UDP


get FileChannel from FileInputStream, FileOutputStream, and RandomAccessFile.

int read(ByteBuffer buf); return the count of bytes read.

int write(ByteBuffer buf) return the count of bytes written.

void close()

void force(boolean b) force to refresh to disk

SocketChannel & ServerSocketChannel

configureBlocking false non-blocking, true blocking

get socket channel

SocketChannel socketChannel =;
socketChannel.connect(new InetSocketAddress("",89));


ServerSocketChannel server = (ServerSocketChannel);
SocketChannel socketChannel = server.accept();

read SocketChannel data*

ByteBuffer buffer = ByteBuffer.allocate(1024);

// >0 length of data read
// 0 non data
// -1 finish flag
int bytesRead =;

write SocketChannel data


close SocketChannel



get datagramChannel

DatagramChannel channel =;
//accept data
channel.socket().bind(new InetSocketAddress(18080));

read datagramChannel data

ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketAddress clientAddress = datagramChannel.receive(buffer);

write datagramChannel data

channel.send(buffer, new InetSocketAddress("",90));

close datagramChannel


NIO Selector

select model

IO event type

  1. SelectionKey.OP_READ (1 « 0)
  2. SelectionKey.OP_WRITE (1 « 1)
  3. SelectionKey.OP_CONNECT (1 « 2)
  4. SelectionKey.OP_ACCEPT (1 « 3)

monitor multiple events by using ‘&’

only classes extends SelectableChannel can be selected (FileChannel cannot be selected)

Selector usage process

Selector selector =;

serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

While ( > 0){
    Set selectedKeys = selector.selectedKeys();
    Iterator keyIterator = selectedKeys.iterator();
        SelectionKey key =;
        // do something


select() had multiple overload

  1. select() blocking
  2. select(long timeout)
  3. selectNow() non-blocking


attach() attach a handler attachment() get the handler

One thread reactor

when is OP_ACCEPT, register and attach the key.
when is OP_READ, do something.

Multiple threads reactor

use thread pool, separate IO event thread and process thread.

IO event thread

create the same counts of Selector and Reactor Thread.

every thread works for a Selector to search and select.

Tack thread

use thread pool to process tasks.



Runnable with return value

public  interface Callable<V>{
    V call() throw Exception;


FutureTask implements RunnableFuture extends Runnable and Future.

public FutureTask(Callable<V> callable){
    if(callable == null)
        throw new NullPointerException();
    this.callable = callable;
    this.state = new;

FutureTask class can be used as Thread class’s target (Runnable), execute async.

FutureTask has an attribute outcome to save result. There is a method get() to get outcome.

Future interface

public interface Future<V>{
    boolean cancel(boolean mayInterruptZRunning);
    boolean isCancelled();
    boolean isDone();
    V get() throws InterruptedException, ExecutionException;
    V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;