Java NIO1

发现了一个很好的学习Java的外国网站,英语都是很简单的啦,看英语舒服些,关于NIO的系列就直接参照此网站了,而且是英语的!

http://tutorials.jenkov.com/

Java NIO (New IO,也有人叫非阻塞IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standard Java IO and Java Networking API‘s. Java NIO offers a different way of working with IO than the standard IO API‘s.

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.

Java NIO consist of the following core components:

  • Channels
  • Buffers
  • Selectors

Java NIO has more classes and components than these, but the Channel, Buffer 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.

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 Channel. Here is an illustration of that:

在IO中会有这样的语句

byte[] buffer = new byte[1024];
InputStream inputStream = new FileInputStream(new File("c:/data.txt"));
inputStream.read(buffer);//inputStream就相当于NIO中的Channel
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.

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 this Buffer out of this overview though.

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

时间: 2024-10-26 20:01:04

Java NIO1的相关文章

Java NIO1:概述

前言 相比I/O,NIO更复杂.更不好理解,因此在开始NIO之前,需要讲解一些概念,如果对于这些概念有着良好的理解,对于学习NIO绝对是有好处的. 同步与异步 所谓同步就是指一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能完成,这是一种可靠的任务序列.要成功都成功,要失败都失败,两个任务的状态可以保持一致. 而异步不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了.至于被依赖的任务最终是否真正完成

Java NIO1:浅谈I/O模型

一.什么是同步?什么是异步? 同步和异步的概念出来已经很久了,网上有关同步和异步的说法也有很多.以下是我个人的理解: 同步就是:如果有多个任务或者事件要发生,这些任务或者事件必须逐个地进行,一个事件或者任务的执行会导致整个流程的暂时等待,这些事件没有办法并发地执行: 异步就是:如果有多个任务或者事件发生,这些事件可以并发地执行,一个事件或者任务的执行不会导致整个流程的暂时等待. 这就是同步和异步.举个简单的例子,假如有一个任务包括两个子任务A和B,对于同步来说,当A在执行的过程中,B只有等待,直

Java NIO1:I/O模型概述

I/O模型 在开始NIO的学习之前,先对I/O的模型有一个理解,这对NIO的学习是绝对有好处的.我画一张图,简单表示一下数据从外部磁盘向运行中进程的内存区域移动的过程: 这张图片明显忽略了很多细节,只涉及了基本操作,下面分析一下这张图. 用户空间和内核空间 一个计算机通常有一定大小的内存空间,如一台计算机有4GB的地址空间,但是程序并不能完全使用这些地址空间,因为这些地址空间是被划分为用户空间和内核空间的.程序只能使用用户空间的内存,这里所说的使用是指程序能够申请的内存空间,并不是真正访问的地址

io_module

io_module 来源: [原创链接: http://www.smithfox.com/?e=191%EF%BB%BF, 转载请保留此声明, 谢谢! ] 异步和同步 同步(synchronous?), 异步?(asynchronous?)?, 阻塞(blocking) 和 非阻塞(non-blocking).  同步阻塞  同步非阻塞  异步阻塞  异步非阻塞 异步就是异步! 只有同步时才有阻塞和非阻塞之分. 阻塞和非阻塞? 我们说 阻塞和 非阻塞 时, 要区分场合范围, 比如 Lin

Java I/O 操作及优化建议

Java I/O I/O,即 Input/Output(输入/输出) 的简称.就 I/O 而言,概念上有 5 种模型:blocking I/O,nonblocking I/O,I/O multiplexing (select and poll),signal driven I/O (SIGIO),asynchronous I/O (the POSIX aio_functions).不同的操作系统对上述模型支持不同,UNIX 支持 IO 多路复用.不同系统叫法不同,freebsd 里面叫 kque

Java I/O不迷茫,一文为你导航!

前言:在之前的面试中,每每问到关于Java I/O 方面的东西都感觉自己吃了大亏..所以这里抢救一下..来深入的了解一下在Java之中的 I/O 到底是怎么回事..文章可能说明类的文字有点儿多,希望能耐心读完.. 什么是 I/O? 学习过计算机相关课程的童鞋应该都知道,I/O 即输入Input/ 输出Output的缩写,最容易让人联想到的就是屏幕这样的输出设备以及键盘鼠标这一类的输入设备,其广义上的定义就是:数据在内部存储器和外部存储器或其他周边设备之间的输入和输出: 我们可以从定义上看到问题的

【Java】 BIO与NIO以及AIO分析

一.BIO与NIO以及AIO的概念 BIO是同步阻塞式的IO NIO是同步非阻塞的IO (NIO1.0,JDK1.4) AIO是非同步非阻塞的IO(NIO2.0,JDK1.7) 二.BIO简单分析 1.简单分析 BIO是阻塞的IO,原因在于accept和read会阻塞.所以单线程的BIO是无法处理并发的. 2.案例 服务端: public class BioServer { public static void main(String[] args) throws IOException {//

Java多线程学习(吐血超详细总结)

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么要用join方法 七常见线程名词解释 八线程同步 九线程数据传递 本文主要讲了java中多线程的使用方法.线程同步.线程数据传递.线程状态及相应的一些线程函数用法.概述等. 首先讲一下进程和线程

Java TM 已被阻止,因为它已过时需要更新的解决方法

公司的堡垒机需要通过浏览器登陆,且该堡垒机的网站需要Java的支持,最近通过浏览器登陆之后总是提示"java TM 已被阻止,因为它已过时需要更新的解决方法"导致登陆之后不能操作, 但是操作系统中确实已经安装了比较新的JDK,安装的JDK版本是jdk-7u67-windows-i586,因为太烦人,所以决定搞清楚报错的原因,一劳永逸,彻底解决这个问题 准备工作:安装JDK,安装版本jdk-7u67-windows-i586.exe,因为机器的Eclipse还依赖64位的JDK,所以另安