NIO Concepts

From NovaOrdis Knowledge Base
Revision as of 21:28, 19 January 2016 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

Internal

Overview

NIO (Non-blocking IO) was introduced in Java 4 and enhanced with new File operations as NIO.2 in Java 7.

The major improvement introduced by NIO was to allow non-blocking I/O operations from Java programs.

Until NIO, all that was available for I/O were Streams (java.io.*), and all operations with Streams are blocking: a thread waits until there is data to read from the Stream instance or until it can write to the Stream instance, so simultaneously handling multiple sources of data (concurrent network connections, for example) required multiple threads that would usually spend most of their time blocked waiting on I/O events. This is the I/O and threading model Tomcat is built on. NIO offers access to underlying O/S non-blocking I/O facilities and offers a mechanism (selectors) through which a single thread could be notified and process I/O events arriving from multiple sources.

Also, because the underlying operating system I/O facilities are block-oriented for efficiency - hardware usually transfers data in blocks rather than byte by byte - NIO exposes block-level access via Channels and Buffers. Data can be read and written in blocks via Buffers into and from the Channels. Channels are also sources of asynchronous I/O events that are routed by selectors into the application. The data transfer between Channels and Buffers is done transparently, without requiring application threads to move bytes around. An application thread is notified once the transfer had completed via an I/O event. The NIO API does not do anything that the Stream API can't do - essentially reading and writing data from/to I/O devices - but it does it faster and using less threads.

Somewhat unrelated, NIO offers new features such as file locking and characters sets, and NIO.2 comes with a new file system access API.

Channel

A Channel represents an open connection to an entity such as a hardware device, a file, a network socket or a program component that is capable of performing I/O operations. The Channel is essentially a source of I/O events, the application does not read or write data from/to the Channel directly, it does so via Buffers.

A channel is either opened or closed. A channel is open upon creation and once closed it remains closed.

Chanel are in general intended to be safe for multithreaded access.

Buffer

java.nio.Buffer is a linear, finite sequence of elements of a specific primitive type. Networking software uses ByteBuffers.

NIO Buffer Mechanics

Selector