摘要

通过温故知新-快速理解Linux网络IO的回顾,我们了解Linux下网络编程的5种I/O模型&I/O多路复用,接下来回顾一下java中的I/O模型,包括BIO、NIO、AIO,为下一篇netty做铺垫。

传统的BIO编程

传统的BIO通信模型



问题

  • 该模型最大的问题就是,客户端的线程个数和客户端的并发呈1:1的关系,线程切换将满满拖垮整个系统;
  • 测试代码如下
---------- server ----------
@Log4j2
public class BioServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9090);
while (true) {
log.info("-- serverSocket before accept --");
Socket socket = serverSocket.accept();
log.info("-- serverSocket end accept --");
new Thread(() -> {
try {
// 读内容
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readLine = bufferedReader.readLine();
log.info("thread:{} client :{}", Thread.currentThread().getName(), readLine);
} catch (IOException e) {
log.error(e);
}
}).run();
}
}
} ---------- client ----------
@Log4j2
public class BioClient {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
Integer tmp = i;
new Thread(() -> {
try {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.write("client message index: " + tmp);
printWriter.flush();
log.info("index:{}", tmp);
} catch (Exception e) {
log.error(e);
}
}).run();
} }
}

伪异步I/O编程

  • 为了解决线程耗尽的问题,引入了线程池,没本质区别;

NIO编程

NIO库是在JDK1.4引进的,弥补了原来同步阻塞I/O的不足,先看一下通信模型,直观的感受一下;



先了解三个概念:缓冲区(buffer)、通道(channel)、多路复用器(Selector)

  • 缓冲区(buffer)

在NIO厍中,所有数据都是用缓冲区处理的。在读取数据时,它是直接读到缓冲区中的; 在写入数据时,写入到缓冲区中。任何时候访问NIO中的数据,都是通过缓冲区进行操作。

类型的缓存有很多种,eg:

  • 通道(channel)

channel是一个通道,网络数据通过channel读取和写入;通道是双向的,流是单项的,流只是在一个方向移动(输入、输出),通道是双工的,可以同时读写,这个跟Unix TCP socket也是一致的;

  • Selector
  • 从上面的图中,最重要的就是Selector,这就是java实现I/O多路复用的核心;
  • 一个Selector可以轮询多个注册到Selector的channel,如果某一个channel发送读写事件,channel就处于了就绪状态,就会被Selector轮询出来,然后通过SelectionKey获取就是Channel就绪集合,由于JDK使用了epoll()代替了传统的select轮询,所以没有1024的句柄限制。
  • 跟BIO和伪异步IO相比,只用一个线程负责轮询,就可以接入成千上完的客户端,所以打好基础建设多么的重要!!
  • 流程

  • 测试代码
--------- server -----
@Log4j2
public class NioServer { public static void main(String[] args) throws Exception { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(9090));
serverSocketChannel.configureBlocking(false); Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); new Thread(() -> {
try {
while (true) {
if (selector.select(1) > 0) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
SelectionKey key = null;
while (it.hasNext()) {
key = it.next();
try {
handle(key, selector);
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null) {
key.channel().close();
}
}
} finally {
it.remove();
}
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}).start();
} private static void handle(SelectionKey key, Selector selector) throws IOException { if (key.isValid()) {
// 处理新接入的请求消息
if (key.isAcceptable()) {
log.info("new channel ...");
// Accept the new connection
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
// Add the new connection to the selector
sc.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
// Read the data
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
String data = new String(bytes, "UTF-8");
log.info("data:{}", data);
} else if (readBytes < 0) {
// 对端链路关闭
key.cancel();
sc.close();
}
}
}
}
}

AIO编程

NIO 2.0引入了新的异步通的概念,提供了异步文件通道和异步套接字通道的实现。

  • 通过juc的Future表示异步操作的结果
  • 在执行异步操作时传入channels
  • CompletionHandler接口实现类作为操作完成的回调。

它是真正的异步非阻塞的IO模型,对应UNIX网络编程重的事件驱动I/O,不需要Selector对注册的通道进行轮询。

  • 测试代码
@Log4j2
public class AioServer { public static void main(String[] args) throws Exception { AsynchronousServerSocketChannel channel = AsynchronousServerSocketChannel
.open();
channel.bind(new InetSocketAddress(9090)); channel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(final AsynchronousSocketChannel asynchronousSocketChannel, Void attachment) {
channel.accept(null, this); ByteBuffer buffer = ByteBuffer.allocate(1024);
asynchronousSocketChannel.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result_num, ByteBuffer attachment) {
attachment.flip();
CharBuffer charBuffer = CharBuffer.allocate(1024);
CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
decoder.decode(attachment, charBuffer, false);
charBuffer.flip();
String data = new String(charBuffer.array(), 0, charBuffer.limit());
log.info("data:{}", data);
try {
asynchronousSocketChannel.close();
} catch (Exception e) {
log.info(e);
}
} @Override
public void failed(Throwable exc, ByteBuffer attachment) {
log.info("read error");
}
});
} @Override
public void failed(Throwable exc, Void attachment) {
System.out.println("accept error");
}
});
while (true){
Thread.sleep(1000);
}
}
}

几种IO模型的对比

netty

  • 下一篇就轮到!

参考

-《Netty 权威指南》第二版 – 李林峰

-《netty实战》–何品


你的鼓励也是我创作的动力

打赏地址

温故知新-java的I/O模型-BIO&NIO&AIO的更多相关文章

  1. JAVA-IO模型(BIO,NIO,AIO)

    基本概念 阻塞和非阻塞 阻塞是进行读写时, 如果当时没有东西可读,或者暂时不可写, 程序就进入等待, 直到有东西可读或者可写为止 非阻塞是如果没有东西可读, 或者不可写, 读写函数马上返回, 而不会等 ...

  2. (转)也谈BIO | NIO | AIO (Java版)

    原文地址: https://my.oschina.net/bluesky0leon/blog/132361 关于BIO | NIO | AIO的讨论一直存在,有时候也很容易让人混淆,就我的理解,给出一 ...

  3. 也谈BIO | NIO | AIO (Java版--转)

    关于BIO | NIO | AIO的讨论一直存在,有时候也很容易让人混淆,就我的理解,给出一个解释: BIO | NIO | AIO,本身的描述都是在Java语言的基础上的.而描述IO,我们需要从两个 ...

  4. I/O模型系列之三:IO通信模型BIO NIO AIO

    一.传统的BIO 网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请 ...

  5. java BIO/NIO/AIO 学习

    一.了解Unix网络编程5种I/O模型 1.1.阻塞式I/O模型 阻塞I/O(blocking I/O)模型,进程调用recvfrom,其系统调用直到数据报到达且被拷贝到应用进程的缓冲区中或者发生错误 ...

  6. Java提供了哪些IO方式?IO, BIO, NIO, AIO是什么?

    IO一直是软件开发中的核心部分之一,而随着互联网技术的提高,IO的重要性也越来越重.纵观开发界,能够巧妙运用IO,不但对于公司,而且对于开发人员都非常的重要.Java的IO机制也是一直在不断的完善,以 ...

  7. 3. 彤哥说netty系列之Java BIO NIO AIO进化史

    你好,我是彤哥,本篇是netty系列的第三篇. 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识. 简介 上一章我们介绍了IO的五种模型,实际上Java只支持其中的三种,即BIO/NIO/ ...

  8. JAVA中的BIO,NIO,AIO

    在了解BIO,NIO,AIO之前先了解一下IO的几个概念: 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪, 例如自己亲自出马持银行卡到银行取钱 2.异步 用户触发IO操作以后, ...

  9. BIO | NIO | AIO (Java版)

    几篇解释的不错的文章: BIO NIO AIO NIO.2 入门,第 1 部分: 异步通道 API 使用异步 I/O 大大提高应用程序的性能

随机推荐

  1. 关于vue打包后,访问不到页面和访问不到图片。

    vue项目完成打包上线的时候很多人都会碰到静态资源找不到,常见的有两个第一种:js,css路径不对解决办法:打开config/index.js,将其中的assetsPublicPath值改为’./’ ...

  2. 力扣题解-LCP 06. 拿硬币

    题目描述 桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释: ...

  3. Django之Middleware中间件方法使用

    自定义中间件五个方法(部分方法)实例 自定义中间件项目: 模板Templates login.html {% load static %} <!DOCTYPE html> <html ...

  4. matlab第六章数据分析与多项式计算

    MATLAB练习 第六章数据分析与多项式计算 1.max和min 1.分别求矩阵A中各列和各行元素中的最大值.max和min的用法一样 % [例6.1]分别求矩阵中各列和各行元素中的最大值. A=[5 ...

  5. Kubernetes fabric8 JavaAPI

    Kubernetes fabric8 JavaAPI 一.依赖准备 <dependency> <groupId>io.fabric8</groupId> <a ...

  6. 从无到有Springboot整合Spring-data-jpa实现简单应用

    本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...

  7. 【Redis】linux中 使用yum下载并安装redis

    1.检查是否有redis yum 源 yum install redis 2.下载fedora的epel仓库 yum install epel-release 3.安装redis数据库 yum ins ...

  8. 容器技术之Docker网络

    上一篇博客我们主要聊了下docker镜像相关的说明以及怎样基于现有镜像制作镜像.分发镜像到docker仓库中的相关测试:回顾请参考https://www.cnblogs.com/qiuhom-1874 ...

  9. ATX-UI自动化环境搭建

    基础环境准备(以下都是在Mac机上搭建的) 1.android sdk安装&配置 很完美的一个资源下载网:tools.android-studio.org,下载所需的包(我下的zip包直接解压 ...

  10. 关于Java两点需要更新的知识

    HashMap的初始容量 背景 很多人可以把HashMap的原理描述的很溜.比如JDK1.7之前,底层数据结构是数组+链表.JDK1.8之后,出于效率上的考虑,在数组长度大于64,链表长度大于8的时候 ...