NIO和AIO
2016-08-23 18:35
323 查看
摘要
本系列基于炼数成金课程,为了更好的学习,做了系列的记录。 本文主要介绍:1. 什么是NIO2. Buffer3. Channel4. 网络编程5. AIO
IO感觉上和多线程并没有多大关系,但是NIO改变了线程在应用层面使用的方式,也解决了一些实际的困难。而AIO是异步IO和前面的系列也有点关系。在此,为了学习和记录,也写一篇文章来介绍NIO和AIO。
NIO是基于块(Block)的,它以块为基本单位处理数据 (硬盘上存储的单位也是按Block来存储,这样性能上比基于流的方式要好一些)
为所有的原始类型提供(Buffer)缓存支持
增加通道(Channel)对象,作为新的原始 I/O 抽象
支持锁(我们在平时使用时经常能看到会出现一些.lock的文件,这说明有线程正在使用这把锁,当线程释放锁时,会把这个文件删除掉,这样其他线程才能继续拿到这把锁)和内存映射文件的文件访问接口
提供了基于Selector的异步网络I/O
所有的从通道中的读写操作,都要经过Buffer,而通道就是io的抽象,通道的另一端就是操纵的文件。
Java中Buffer的实现。基本的数据类型都有它对应的Buffer
Buffer的简单使用例子:
总结下使用的步骤是:
1. 得到Channel
2. 申请Buffer
3. 建立Channel和Buffer的读/写关系
4. 关闭
下面的例子是使用NIO来复制文件:
Buffer中有3个重要的参数:位置(position)、容量(capactiy)和上限(limit)
这里要区别下容量和上限,比如一个Buffer有10KB,那么10KB就是容量,我将5KB的文件读到Buffer中,那么上限就是5KB。
下面举个例子来理解下这3个重要的参数:
整个过程如图:
此时position从0到10,capactiy和limit不变。
该操作会重置position,通常,将buffer从写模式转换为读 模式时需要执行此方法 flip()操作不仅重置了当前的position为0,还将limit设置到当前position的位置 。
limit的意义在于,来确定哪些数据是有意义的,换句话说,从position到limit之间的数据才是有意义的数据,因为是上次操作的数据。所以flip操作往往是读写转换的意思。
意义同上。
而Buffer中大多数的方法都是去改变这3个参数来达到某些功能的:
将position置零,并清除标志位(mark)
将position置零,同时将limit设置为capacity的大小,并清除了标志mark
先将limit设置到position所在位置,然后将position置零,并清除标志位mark,通常在读写转换时使用
对MappedByteBuffer的修改就相当于修改文件本身,这样操作的速度是很快的。
简单的多线程服务器:
功能就是服务器端读到什么数据,就向客户端回写什么数据。
这里的tp是一个线程池,HandleMsg是处理消息的类。
客户端:
以上的网络编程是很基本的,使用这种方式,会有一些问题:
为每一个客户端使用一个线程,如果客户端出现延时等异常,线程可能会被占用很长时间。因为数据的准备和读取都在这个线程中。
因为
是阻塞的,所以时间都花在等待中。
如果用NIO来处理这个问题会怎么做呢?
NIO有一个很大的特点就是:把数据准备好了再通知我
而Channel有点类似于流,一个Channel可以和文件或者网络Socket对应 。
本系列基于炼数成金课程,为了更好的学习,做了系列的记录。 本文主要介绍:1. 什么是NIO2. Buffer3. Channel4. 网络编程5. AIO
IO感觉上和多线程并没有多大关系,但是NIO改变了线程在应用层面使用的方式,也解决了一些实际的困难。而AIO是异步IO和前面的系列也有点关系。在此,为了学习和记录,也写一篇文章来介绍NIO和AIO。
1. 什么是NIO
NIO是New I/O的简称,与旧式的基于流的I/O方法相对,从名字看,它表示新的一套Java I/O标 准。它是在Java 1.4中被纳入到JDK中的,并具有以下特性:NIO是基于块(Block)的,它以块为基本单位处理数据 (硬盘上存储的单位也是按Block来存储,这样性能上比基于流的方式要好一些)
为所有的原始类型提供(Buffer)缓存支持
增加通道(Channel)对象,作为新的原始 I/O 抽象
支持锁(我们在平时使用时经常能看到会出现一些.lock的文件,这说明有线程正在使用这把锁,当线程释放锁时,会把这个文件删除掉,这样其他线程才能继续拿到这把锁)和内存映射文件的文件访问接口
提供了基于Selector的异步网络I/O
所有的从通道中的读写操作,都要经过Buffer,而通道就是io的抽象,通道的另一端就是操纵的文件。
2. Buffer
Java中Buffer的实现。基本的数据类型都有它对应的Buffer
Buffer的简单使用例子:
package test; import java.io.File; import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) throws Exception { FileInputStream fin = new FileInputStream(new File( "d:\\temp_buffer.tmp")); FileChannel fc = fin.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); fc.read(byteBuffer); fc.close(); byteBuffer.flip();//读写转换 } }
总结下使用的步骤是:
1. 得到Channel
2. 申请Buffer
3. 建立Channel和Buffer的读/写关系
4. 关闭
下面的例子是使用NIO来复制文件:
public static void nioCopyFile(String resource, String destination) throws IOException { FileInputStream fis = new FileInputStream(resource); FileOutputStream fos = new FileOutputStream(destination); FileChannel readChannel = fis.getChannel(); // 读文件通道 FileChannel writeChannel = fos.getChannel(); // 写文件通道 ByteBuffer buffer = ByteBuffer.allocate(1024); // 读入数据缓存 while (true) { buffer.clear(); int len = readChannel.read(buffer); // 读入数据 if (len == -1) { break; // 读取完毕 } buffer.flip(); writeChannel.write(buffer); // 写入文件 } readChannel.close(); writeChannel.close(); }
Buffer中有3个重要的参数:位置(position)、容量(capactiy)和上限(limit)
这里要区别下容量和上限,比如一个Buffer有10KB,那么10KB就是容量,我将5KB的文件读到Buffer中,那么上限就是5KB。
下面举个例子来理解下这3个重要的参数:
public static void main(String[] args) throws Exception { ByteBuffer b = ByteBuffer.allocate(15); // 15个字节大小的缓冲区 System.out.println("limit=" + b.limit() + " capacity=" + b.capacity() + " position=" + b.position()); for (int i = 0; i < 10; i++) { // 存入10个字节数据 b.put((byte) i); } System.out.println("limit=" + b.limit() + " capacity=" + b.capacity() + " position=" + b.position()); b.flip(); // 重置position System.out.println("limit=" + b.limit() + " capacity=" + b.capacity() + " position=" + b.position()); for (int i = 0; i < 5; i++) { System.out.print(b.get()); } System.out.println(); System.out.println("limit=" + b.limit() + " capacity=" + b.capacity() + " position=" + b.position()); b.flip(); System.out.println("limit=" + b.limit() + " capacity=" + b.capacity() + " position=" + b.position()); }
整个过程如图:
此时position从0到10,capactiy和limit不变。
该操作会重置position,通常,将buffer从写模式转换为读 模式时需要执行此方法 flip()操作不仅重置了当前的position为0,还将limit设置到当前position的位置 。
limit的意义在于,来确定哪些数据是有意义的,换句话说,从position到limit之间的数据才是有意义的数据,因为是上次操作的数据。所以flip操作往往是读写转换的意思。
意义同上。
而Buffer中大多数的方法都是去改变这3个参数来达到某些功能的:
public final Buffer rewind()
将position置零,并清除标志位(mark)
public final Buffer clear()
将position置零,同时将limit设置为capacity的大小,并清除了标志mark
public final Buffer flip()
先将limit设置到position所在位置,然后将position置零,并清除标志位mark,通常在读写转换时使用
文件映射到内存
public static void main(String[] args) throws Exception { RandomAccessFile raf = new RandomAccessFile("C:\\mapfile.txt", "rw"); FileChannel fc = raf.getChannel(); // 将文件映射到内存中 MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); while (mbb.hasRemaining()) { System.out.print((char) mbb.get()); } mbb.put(0, (byte) 98); // 修改文件 raf.close(); }
对MappedByteBuffer的修改就相当于修改文件本身,这样操作的速度是很快的。
3. Channel
多线程网络服务器的一般结构:简单的多线程服务器:
public static void main(String[] args) throws Exception { ServerSocket echoServer = null; Socket clientSocket = null; try { echoServer = new ServerSocket(8000); } catch (IOException e) { System.out.println(e); } while (true) { try { clientSocket = echoServer.accept(); System.out.println(clientSocket.getRemoteSocketAddress() + " connect!"); tp.execute(new HandleMsg(clientSocket)); } catch (IOException e) { System.out.println(e); } } }
功能就是服务器端读到什么数据,就向客户端回写什么数据。
这里的tp是一个线程池,HandleMsg是处理消息的类。
static class HandleMsg implements Runnable{ 省略部分信息 public void run(){ try { is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); os = new PrintWriter(clientSocket.getOutputStream(), true); // 从InputStream当中读取客户端所发送的数据 String inputLine = null; long b=System. currentTimeMillis (); while ((inputLine = is.readLine()) != null) { os.println(inputLine); } long e=System. currentTimeMillis (); System. out.println ("spend:"+(e - b)+" ms "); } catch (IOException e) { e.printStackTrace(); }finally { 关闭资源 } } }
客户端:
public static void main(String[] args) throws Exception { Socket client = null; PrintWriter writer = null; BufferedReader reader = null; try { client = new Socket(); client.connect(new InetSocketAddress("localhost", 8000)); writer = new PrintWriter(client.getOutputStream(), true); writer.println("Hello!"); writer.flush(); reader = new BufferedReader(new InputStreamReader( client.getInputStream())); System.out.println("from server: " + reader.readLine()); } catch (Exception e) { } finally { // 省略资源关闭 } }
以上的网络编程是很基本的,使用这种方式,会有一些问题:
为每一个客户端使用一个线程,如果客户端出现延时等异常,线程可能会被占用很长时间。因为数据的准备和读取都在这个线程中。
spend:6000ms spend:6000ms spend:6000ms spend:6001ms spend:6002ms spend:6002ms spend:6002ms spend:6002ms spend:6003ms spend:6003ms
因为
while ((inputLine = is.readLine()) != null)
是阻塞的,所以时间都花在等待中。
如果用NIO来处理这个问题会怎么做呢?
NIO有一个很大的特点就是:把数据准备好了再通知我
而Channel有点类似于流,一个Channel可以和文件或者网络Socket对应 。
相关文章推荐
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- JAVA 中BIO,NIO,AIO的理解
- JAVA中IO技术:BIO、NIO、AIO
- JAVA 中BIO,NIO,AIO的理解
- NIO,BIO,AIO,JAVA通讯编程学习笔记3
- BIO与NIO、AIO
- Java BIO,NIO,AIO的区别
- NIO和AIO
- Java中NIO、BIO、AIO相关概念及应用场景
- 也谈BIO | NIO | AIO (Java版--转)
- BIO,NIO,AIO的区别
- Java BIO, NIO, AIO 总结
- BIO,NIO,AIO的区别
- BIO ,NIO ,AIO
- Java BIO NIO AIO
- BIO、NIO、AIO
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- java BIO NIO AIO 理论篇
- 五种I/O 模式,select、epoll方法的理解,BIO、NIO、AIO理解 相关文章