標籤:bsd offer char lse zip bim connect body java
Java NIO Tutorial
|
Jakob Jenkov Last update: 2014-06-25 |
Java NIO (New IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standardJava IO and Java Networking API‘s. Java NIO offers a different way of working with IO than the standard IO API‘s.
NIO是一個可供替代的 io API, 可以用來代替原來的IO 和網路功能 API。
Java NIO: Channels and Buffers
In the standard IO API you work with byte streams and character streams. In NIO you work with channels and buffers. Data is always read from a channel into a buffer, or written from a buffer to a channel.
標準IO api 是用 位元組流和 字元流工作;NIO是用 通道 Channel和緩衝 buffer 工作,資料是從Channel讀到buffer,從buffer寫到Channel
Java NIO: Non-blocking IO
nio是非阻塞io,thread讓Channel讀資料到buffer,然後線程可以做其他事,讀完了可以繼續處理他。
Java NIO enables you to do non-blocking IO. For instance, a thread can ask a channel to read data into a buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it. The same is true for writing data to channels.
Java NIO: Selectors
一個selector可以類比操作多個Channel的處理資料。
Java NIO contains the concept of "selectors". A selector is an object that can monitor multiple channels for events (like: connection opened, data arrived etc.). Thus, a single thread can monitor multiple channels for data.
How all this works is explained in more detail in the next text in this series - the Java NIO overview.
Java NIO Tutorial