源自:http://tutorials.jenkov.com/java-nio/overview.html#selectors
Java NIO consist of the following core components:
- Channels
- Buffers
- Selectors
JAVA NIO由以下核心組件組成:Channels、Buffers、Selectors。
Java NIO has more classes and components than these, but the Channel
, Buffer
and Selector
forms the core of the API, in my opinion. The rest of the components, like Pipe
and FileLock
are merely utility classes to be used in conjunction with the three core components. Therefore, I'll focus on these three components in this NIO overview. The other components are explained in their own texts elsewhere in this tutorial. See the menu at the top corner of this page.
除了上面三個,Java NIO還有更多的類和組件,但是依我看來,通道、緩衝區和選擇器構成了API的核心。其餘的組件,如Pipe和FileLock只是與三個核心組件結合使用的實用工具類。因此,我將在這個NIO概述中重點介紹這三個組件。其他組件將在本教程的其他地方的文本中解釋。請參閱本頁上角的菜單。
Channels and Buffers
Typically, all IO in NIO starts with a Channel
. A Channel
is a bit like a stream. From the Channel
data can be read into a Buffer
. Data can also be written from a Buffer
into a Channel
. Here is an illustration of that:
通常,NIO中的所有IO都以通道開始。通道有點像溪流。從通道取數據放到緩衝區。數據也可以從緩衝區寫入通道。這是一個例子:
Java NIO: Channels read data into Buffers, and Buffers write data into Channels
There are several Channel
and Buffer
types. Here is a list of the primary Channel
implementations in Java NIO:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
有幾種通道和緩衝區類型。下面是Java NIO中的通道實現列表:FIleChannel、DatagramChannel、SocketChannel、ServerSocketChannel。
As you can see, these channels cover UDP + TCP network IO, and file IO.
如你所見,這些通道包括UDP+TCP網絡IO和文件IO。
There are a few interesting interfaces accompanying these classes too, but I'll keep them out of this Java NIO overview for simplicity's sake. They'll be explained where relevant, in other texts of this Java NIO tutorial.
這些類也有一些有趣的接口。但爲了簡單起見,我將把它們保留在這個Java NIO概述中。在JavaNIO教程的其他文本中,它們將被解釋爲相關的內容。
Here is a list of the core Buffer
implementations in Java NIO:
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
下面是Java NIO中核心緩衝區實現的列表:ByteBuffer、CharBuffer、DoubleBuffer、FloatBuffer、IntBuffer、LongBuffer、ShortBuffer。
These Buffer
's cover the basic data types that you can send via IO: byte, short, int, long, float, double and characters.
Java NIO also has a MappedByteBuffer
which is used in conjunction with memory mapped files. I'll leave this Buffer
out of this overview though.
這些緩衝區包括可以通過IO發送的基本數據類型:byte、short、int、long、float、double、characters。Java NIO也有一個MappedByteBuffer
,它與內存映射文件一起使用。不過,我將把這個緩衝區從這個概述中去掉。
Selectors
A Selector
allows a single thread to handle multiple Channel
's. This is handy if your application has many connections (Channels) open, but only has low traffic on each connection. For instance, in a chat server.
Here is an illustration of a thread using a Selector
to handle 3 Channel
's:
選擇器允許單個線程處理多個通道。如果你的應用程序打開了許多連接(通道),這很方便,但每個連接上的流量都很低。例如,在聊天服務器中。下面是使用選擇器處理3通道的線程示例:
To use a Selector
you register the Channel
's with it. Then you call it's select()
method. This method will block until there is an event ready for one of the registered channels. Once the method returns, the thread can then process these events. Examples of events are incoming connection, data received etc.
要使用選擇器,您需要用它註冊通道。然後調用它的select()方法。此方法將一直阻止,直到有一個事件準備就緒,可用於其中一個已註冊的通道。一旦方法返回,線程就可以處理這些事件。事件的例子包括傳入連接、接收的數據等。