Java NIO Tutorial 1- Introduction

Java NIO Tutorial 1- Introduction

Java NIO (New IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standard Java IO API's. Java NIO offers a different way of working with IO than the standard IO API's.

Java NIO 是java从1.4版本开始提供的新IO API,是对标准IO API的一个替代。和标准的IO API相比,NIO提供访问IO的不同的工作方式。


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.

在标准IO API总我们使用byte streams和character streams,在NIO中我们使用channels和buffers。数据总是从channel读入到buffer,或者从buffer写入到channel中。


Java NIO: Asynchronous IO

Java NIO enables you to do asynchronous 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能够异步的访问IO。以一个线程请求从一个channel读数据到buffer中为例。在channel把数据读入到buffer的同时,这个线程能够做一些其他事情(译者注:标准IO是阻塞式访问,当线程访问IO时是被阻塞的,当IO访问完成后,此线程才被唤醒继续执行。)。一旦数据已经读入到了buffer中,这个线程就能够继续处理数据。同样,对于把数据从buffer写到channel也是同理。

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.

Java NIO里有一个重要概念“selector”。一个selector是一个能够监视多个channel事件(比如:连接打开事件,数据到达事件等等)的对象。这样,单独一个线程就能够监视多个channel。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值