Java NIO基本组件Selector详解

85 篇文章 1 订阅

Selector流程原理

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

Selector要点

在这里插入图片描述

Selector 常用方法

在这里插入图片描述

采用Channel和ByteBuffer,Selector,手写tomcat

//采用NIO,Channel和ByteBuffer,Selector
public class tomcatSelector {
    public static final String SEPARATOR = "\r\n";
    //源码注释:requested maximum length of the queue of incoming connections.
    //BACK_LOG影响的accept队列大小
    public static final int BACK_LOG = 1024;

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);
        serverChannel.bind(new InetSocketAddress(10000), BACK_LOG);
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("启动服务器");
        for (; ; ) {
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            System.out.println("----------------------");
            while (iterator.hasNext()) {
                SelectionKey next = iterator.next();
                if (next.isAcceptable()) {
                    ServerSocketChannel serverChannelIn = (ServerSocketChannel) next.channel();
                    SocketChannel acceptChannel = serverChannelIn.accept();
                    System.out.println(acceptChannel.getRemoteAddress());
                    acceptChannel.configureBlocking(false);
                    acceptChannel.register(selector, SelectionKey.OP_WRITE);
                    System.out.println(1);
                }
                if (next.isWritable()) {
                    System.out.println(2);
                    SocketChannel channel = (SocketChannel) next.channel();
                    try {
                        //模拟在做事
                        Thread.sleep(500);
                        channel.write(ByteBuffer.wrap(HttpRequest().getBytes()));
                    } catch (Exception e) {
                        //e.printStackTrace();
                    }
                }
                iterator.remove();
            }
        }
    }

    public static String HttpRequest() {
        String str = "<h1>tomcat</h1>";
        StringBuilder builder = new StringBuilder();
        builder.append("HTTP/1.1 200 OK").append(SEPARATOR);
        builder.append("Connection: Close").append(SEPARATOR);
        builder.append("Content-Type: text/html;charset=utf-8").append(SEPARATOR);
        builder.append("Content-Length: " + str.length()).append(SEPARATOR);
        builder.append(SEPARATOR);
        builder.append(str);
        return builder.toString();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值