NIO Concepts: Difference between revisions
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
NIO (Non-blocking IO) was introduced in Java 4 and enhanced with new File operations as NIO.2 in Java 7. | 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 (<tt>java.io.*</tt>), 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. NIO offers access to underlying O/S non-blocking I/O facilities. | 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 (<tt>java.io.*</tt>), 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. | ||
Revision as of 20:50, 19 January 2016
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.
Buffer
java.nio.Buffer is a linear, finite sequence of elements of a specific primitive type. Networking software uses ByteBuffers.
ByteBuffer
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 one or more distinct I/O operations - for example reading or writing.
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.