Netty学习笔记(自用)

BIO(同步阻塞式IO):

一个连接一个线程,可以使用线程池调优。

连接成功时,线程只能等待用户动作,无法做其他事情,而如果没有数据可以读则会堵塞在READ操作,造成资源浪费。

package BIO;

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 BIOServer {

    public static void main(String[] args) throws IOException {
        //创建一个线程池
        ExecutorService es=Executors.newCachedThreadPool();
        //创建ServcerSocket
        ServerSocket ss=new ServerSocket(6666);
        System.out.print("FW STARTING");
        while (true){
            //监听等待客户端连接
            final Socket socket = ss.accept();
            System.out.println("GET ONE CONNECT");
            //创建一个线程与之通讯
            es.execute(new Runnable() {
                public void run() {
                    try {
                        handler(socket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
    public static void handler(Socket s) throws IOException{
        byte[] bytes = new byte[1024];
        //通过socket 获取输入流
        InputStream inputStream=s.getInputStream();
        while(true){
            int read = inputStream.read(bytes);
            if(read != -1){
                System.out.println(new String(bytes,0,read));
            }else{
                break;
            }
        }
        System.out.println("CLOSE");
        s.close();
    }
}

 

NIO(同步非阻塞式IO)  ,全称 java non-blocking  IO,java1.4后 统称 New IO,面向缓冲区

更多NIO知识见我上篇博客

三大核心:channel 、buffer、selector

IntBuffer intBuffer = IntBuffer.allocate(5);
for(int a=0;a<intBuffer.capacity();i++){
      intBuffer.put(i*2);
}
intBuffer.flip();
while(intBuffer.hasRemarning()){
      System.out.print(intBuffer.get())
}

epoll Bug selector  CUP100%空轮询

AIO(异步非阻塞式IO),还未广泛应用

 

三者应用场景:

 

Netty

 

Reactor 其实就是一个中间件,专门用来进行分发的。

 

 

Reactor 多线程

为了解决上述单Reactor缺点,提出了主从Reactor

 

 

主线程和子线程都可以有多个

 

Netty模型

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值