NIO------翻译/标记

摘录于------NIO

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.

数据总是从通道读到缓冲区,或者从缓冲区写到通道。

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: 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

一个单线程可以监视多个通道的数据。(翻译是否欠妥......)

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: 



                                                                

                    Java NIO: Channels read data into Buffers, and Buffers write data into Channels

     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. 

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:

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

Java NIO Channel

     Java NIO Channels are similar to streams with a few differences:(Java NIO与流的不同之处)

  • You can both read and write to a Channels(全双工?). Streams are typically one-way (read or write).(单工?)
  • Channels can be read and written asynchronously.(异步读写:what does that mean?)
  • Channels always read to, or write from, a Buffer.

Channel Implementations

Here are the most important Channel implementations in Java NIO:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

The FileChannel reads data from and to files.

The DatagramChannel can read and write data over the network via UDP.(通过UDP在网络上读取和写入数据)

The SocketChannel can read and write data over the network via TCP.(通过TCP在网络上读取和写入数据)

The ServerSocketChannel allows you to listen(监听) for incoming TCP connections(传入的TCP连接), like a web server does(就像是网络服务器所做那样......(好像了解的意思不太准确)). For each incoming connection a SocketChannel is created





猜你喜欢

转载自blog.csdn.net/lihai755/article/details/80303922
NIO