java NIO 基礎概念

譯文:Java NIO Tutorial

源自:http://tutorials.jenkov.com/java-nio/index.html

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,你使用通道和緩衝區,往往從通道讀數據放到緩衝區裏面,或者從緩衝區寫入到通道中。

Java NIO: Non-blocking IO

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 不阻塞。比如,當一個線程從通道請求數據放入到緩衝區的時候,它還可以做其他的事情。一旦數據被讀取到緩衝區中,線程就可以繼續處理。向通道寫入數據也是如此。

Java NIO: Selectors

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.

Java NIO包含“選擇器”的概念。選擇器是一個可以監視多個通道的事件的對象(比如連接打開,數據到達等等)。因此,一個線程可以監視多個通道以獲取數據。

 

--如有表述不恰當之處,歡迎道友指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章