Java NIO Tutorial 2- Java NIO Overview

Java NIO Overview

Java NIO consist of the following core components:

  • Channels
  • Buffers
  • Selectors

Java NIO has more classes and components than these, but the ChannelBuffer 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 由以下的核心组件组成:
  • Channels
  • Buffers
  • Selectors
Java NIO包含很多类和组件,但是作者认为Channel,Buffer和Selector才是这些API的核心。而其他的组件,比如Pipe和FileLock,仅仅是用来和这三个核心组件协同工作的工具类。因此,在本节作者只集中介绍这三个核心组件。其他的组件会在合适的章节进行介绍,可以通过查看菜单找到其他组件的介绍。

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操作都由Channel开始。Channel有点类似stream。数据可以从Channel读取到buffer,数据也可以从buffer写入到channel,像下面的图所示意的一样:

Java NIO: Channels and Buffers
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

As you can see, these channels cover UDP + TCP network IO, and file 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.

在NIO中有很多Channel和Buffer的类型,这里列出在NIO里主要的Channel实现类:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel
就如你看到的,这些channel的实现累涵盖了UDP+TCP的网络IO和文件IO。

和这些类相关的还有一些有趣的接口,简单起见就不在本章节介绍了。这些接口会在其他相关的章节中进行介绍。


Here is a list of the core Buffer implementations in 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 thisBuffer out of this overview though.


下面列出了Java NIO中Buffer的实现:
  • ByteBuffer
  • CharBuffer
  • DoubleBuffer
  • FloatBuffer
  • IntBuffer
  • LongBuffer
  • ShortBuffer
这些Buffer的实现涵盖了访问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:

Selector允许一个线程处理多个Channel。如果应用有很多连接(Channel)打开但是每个连接的负荷却很低的情况,比如一个聊天室的服务器,那么这个特性就提供了很大的便利。(译者注:也即是说Selector适用于多连接低负荷的系统)

下图说明了线程使用一个Selector来处理三个Channel的场景:

Java NIO: Selectors
Java NIO: A Thread uses a Selector to handle 3 Channel's


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.


要使用Selector首先必须要把它注册在Channel上,之后调用它的select()方法。这个方法将会一直阻塞,直到被注册的Channel有事件发生。事件发生之后这个方法将会返回,然后线程就可以处理所发生的事件。发生的事件可以是有新的连接接入或者数据到达等等。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值