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

本文深入介绍了Java NIO的基础概念,包括Channels和Buffers的工作原理,非阻塞IO的特点,以及Selectors如何帮助单线程处理多个Channel。同时,文章还列举了核心的Buffer实现以及重要的Channel实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘录于------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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值