NIO Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
 
(92 intermediate revisions by the same user not shown)
Line 6: Line 6:
=Overview=
=Overview=


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.2FO|non-blocking]], block-oriented I/O operations from Java programs. Somewhat unrelated, NIO offers new features such as [[#File_Locking|file locking]] and [[#Character_Sets|characters sets]], and NIO.2 comes with a new [[#NIO_2_File_API|file system access API]].


The major improvement introduced by NIO was to allow non-blocking I/O operations from Java programs.
=<span id='Multiplexed_Non-Blocking_I.2FO_Facility'></span><span id='Selector'></span><span id='Selector_Key'></span><span id='Selectable_Channel'></span><span id='Stream-Oriented_vs._Block-Oriented_I.2FO_Operations'></span><span id='Channel'></span><span id='Buffer'></span><span id='Channel.2FBuffer_Interaction'></span>Non-Blocking I/O=


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.
{{Internal|Java Non-Blocking I/O Concepts#Overview|Java Non-Blocking I/O Concepts}}


NIO offers access to underlying O/S non-blocking I/O facilities and offers a mechanism ([[#Selectors|selectors]]) through which a single thread could be notified and process I/O events arriving from multiple sources. Because the underlying O/S 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 [[#Channel|Channels] and [#Buffer|Buffers]: data can be read and written in blocks via Buffers into the Channels, and the Channels are also sources of asynchronous I/O events that are routed by selectors into the application.
=File Locking=


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[NIO File Locking]]
</blockquote>


=Character Sets=


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[NIO Character Sets]]
</blockquote>


 
=NIO 2=
 
{{Internal|Java NIO 2|Java NIO 2}}
 
 
=Buffer=
 
<tt>java.nio.Buffer</tt> is a linear, finite sequence of elements of a specific primitive type. Networking software uses [[#ByteBuffer|ByteBuffer]]s.
 
=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.
 
=Selector=

Latest revision as of 22:03, 18 June 2020

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, block-oriented I/O operations from Java programs. Somewhat unrelated, NIO offers new features such as file locking and characters sets, and NIO.2 comes with a new file system access API.

Non-Blocking I/O

Java Non-Blocking I/O Concepts

File Locking

NIO File Locking

Character Sets

NIO Character Sets

NIO 2

Java NIO 2