java进阶-Netty

Netty

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

Netty视频教程

Netty demo代码文件

I/O

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

BIO

java进阶-Netty_第1张图片
java进阶-Netty_第2张图片

  • 适用连接数目少且固定的服务
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);
        }
    }
}

java进阶-Netty_第3张图片

NIO

java进阶-Netty_第4张图片

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

AIO

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

NIO原理

java进阶-Netty_第5张图片
java进阶-Netty_第6张图片

java进阶-Netty_第7张图片

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

java进阶-Netty_第8张图片
java进阶-Netty_第9张图片
java进阶-Netty_第10张图片

selectionKey

java进阶-Netty_第11张图片

零拷贝

java进阶-Netty_第12张图片

java进阶-Netty_第13张图片
java进阶-Netty_第14张图片

Reactor

java进阶-Netty_第15张图片

单Reactor单线程

java进阶-Netty_第16张图片

单Reactor多线程

java进阶-Netty_第17张图片

主从Reactor多线程

java进阶-Netty_第18张图片

Netty线程模型
  • 简单版
    java进阶-Netty_第19张图片
  • 详细版

java进阶-Netty_第20张图片

java进阶-Netty_第21张图片

你可能感兴趣的:(java,开发语言)