java进阶-Netty

本文介绍了Netty框架,对比了BIO(阻塞IO)、NIO和AIO的工作原理,以及在不同场景下的适用性。通过FileBuffer和Selector机制,展示了NIO的文件操作和事件驱动。此外,还提到了Reactor模式在Netty中的线程组织。
摘要由CSDN通过智能技术生成

Netty

在此非常感谢尚硅谷学院以及韩顺平老师在B站公开课

Netty视频教程

Netty demo代码文件

I/O

说NIO之前先说一下BIO(Blocking IO),如何理解这个Blocking呢?客户端监听(Listen)时,Accept是阻塞的,只有新连接来了,Accept才会返回,主线程才能继读写socket时,Read是阻塞的,只有请求消息来了,Read才能返回,子线程才能继续处理读写socket时,Write是阻塞的,只有客户端把消息收了,Write才能返回,子线程才能继续读取下一个请求

BIO

在这里插入图片描述
在这里插入图片描述

  • 适用连接数目少且固定的服务
package com.example.demo01.netty;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIODemo01 {

    public static void main(String[] args) throws IOException {

        ExecutorService executorService = Executors.newCachedThreadPool();

        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("服务器启动成功。。。");

        while(true){

            try {
                System.out.println(Thread.currentThread().getName() + " 等待连接正在阻塞。。。。。。");
                final Socket socket = serverSocket.accept();

                executorService.submit(() -> {

                    socketHandler(socket);
                });

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void socketHandler(Socket socket) {

        try {
            InputStream inputStream = socket.getInputStream();

            byte[] bytes = new byte[1024];

            while (true){
                System.out.println(Thread.currentThread().getName() + " 读取数据正在阻塞。。。");
                int read = inputStream.read(bytes);
                if(read != -1){
                    System.out.println(new String(bytes, 0, read));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

在这里插入图片描述

NIO

在这里插入图片描述

  • 适用连接数目多且连接比较短的服务
  • jdk1.4开始支持

AIO

  • 适用连接数目多且连接比较长的服务
  • jdk7开始支持

NIO原理

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

FIleBuffer

package com.example.demo01.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioFileChannel {

    public static void main(String[] args) throws Exception {


        String str = new String("测试文档");
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        allocate.put(str.getBytes());
        allocate.flip();

        FileOutputStream fileOutputStream = new FileOutputStream("d:\\test.txt");
        FileChannel channel = fileOutputStream.getChannel();
        channel.write(allocate);

        fileOutputStream.close();
    }
}

package com.example.demo01.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioFileChannelInputStream {

    public static void main(String[] args) throws IOException {
        File file = new File("d:\\test.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        FileChannel channel = fileInputStream.getChannel();

        ByteBuffer allocate = ByteBuffer.allocate((int) file.length());

        channel.read(allocate);

        System.out.println(new String(allocate.array()));
        fileInputStream.close();

    }
}

package com.example.demo01.netty.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class NioTrancfer {

    public static void main(String[] args) throws Exception {

        FileInputStream fileInputStream = new FileInputStream("d:\\1.jpeg");
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.jpeg");

        FileChannel sourceChannel = fileInputStream.getChannel();
        FileChannel destChannel = fileOutputStream.getChannel();

        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        sourceChannel.close();
        destChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}

selector

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

selectionKey

在这里插入图片描述

零拷贝

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Reactor

在这里插入图片描述

单Reactor单线程

在这里插入图片描述

单Reactor多线程

在这里插入图片描述

主从Reactor多线程

在这里插入图片描述

Netty线程模型
  • 简单版
    在这里插入图片描述
  • 详细版

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值