Channels and Buffers、Selectors

源自: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 ChannelBuffer and Selectorforms 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 Channeldata 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

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()方法。此方法将一直阻止,直到有一个事件准备就绪,可用于其中一个已注册的通道。一旦方法返回,线程就可以处理这些事件。事件的例子包括传入连接、接收的数据等。

 

 

 

 

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