BIO 探究二

接上文 BIO 初探究


前言

提示:验证阻塞到底阻塞在什么地方


提示:以下是本篇文章正文内容,下面案例可供参考

一、使用netty

上文猜想 bio 的阻塞是阻塞在server 端的 while ((len = inputStream.read(content)) != -1) 这行代码上,本文就验证下,验证方案如下

  • 实现一个NIO 服务器
  • 使用多个BIO 客户端连接 NIO 服务器
  • 查看结果

netty 服务端


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

public class NettyServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup boss = new NioEventLoopGroup(1);
		// 处理连接数线程设置为1
        EventLoopGroup worker = new NioEventLoopGroup(1);

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(9090)) //配置监听本地端口
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()
                                    .addLast("handler", new GenerallyServerHandler()); //2.4 加入自定义业务逻辑ChannelHandler
                        } //2.3 初始化channel的时候,配置Handler)
                    })
                    .option(ChannelOption.SO_BACKLOG, 128) //(5)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture portFuture = bootstrap.bind().sync();
            System.out.println("netty server started...");
            // 不加这行代码 服务启动一会就关闭了
            portFuture.channel().closeFuture().sync();
        } finally {
            boss.shutdownGracefully().sync();
            worker.shutdownGracefully().sync();
        }
    }

}

处理器

package com.microsoft.samples.nexo.openprotocol.demo.nio;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class GenerallyServerHandler extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("rev: " + buf.toString(CharsetUtil.UTF_8));
    }
}

使用上文的 TestClient 多个 bio 客户端验证
结果:收到了所有的客户端发来的值
在这里插入图片描述
结论:BIO 的阻塞 在于 io 流的阻塞, 一个网络 io 流 需要一个线程,在流没有结束前,需要当前线程一直阻塞,
问题来了:目前看到的阻塞 都是在这个服务端的 while ((len = inputStream.read(content)) != -1) 这段 代码里边;
我们如果去除掉这行代码呢?
执行结果: 每个客户端发送的消息都收到了,但是只收到了 一条消息(上文的客户端每个都发了两条消息),这样看来阻塞似乎是我们编码上人为的原因造成的阻塞;
或者说网上所谓说的 bio 阻塞 是否也包含 while ((len = inputStream.read(content)) != -1) 这行代码? 有清楚的大佬 麻烦点一点;

二、nio 客户端 与 bio 服务端,bio 服务端 与 nio 客户端


总结

# 未完待续

相关推荐

  1. BIO、NIO

    2024-06-06 06:50:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 06:50:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 06:50:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 06:50:02       87 阅读
  4. Python语言-面向对象

    2024-06-06 06:50:02       97 阅读

热门阅读

  1. WEB三大主流框架之React

    2024-06-06 06:50:02       24 阅读
  2. Nuxt - middleware 路由中间件

    2024-06-06 06:50:02       31 阅读
  3. 007 异步同步

    2024-06-06 06:50:02       22 阅读
  4. DNS域名

    DNS域名

    2024-06-06 06:50:02      28 阅读
  5. oracle 核心进程

    2024-06-06 06:50:02       27 阅读
  6. Oracle通过datax迁移线上表到历史库

    2024-06-06 06:50:02       26 阅读
  7. [Mac软件]Leech for Mac v3.2 - 轻量级mac下载工具

    2024-06-06 06:50:02       23 阅读
  8. Oracle中clob怎么拼接字符

    2024-06-06 06:50:02       31 阅读
  9. 从0开始读C++Primer|第一章 开始

    2024-06-06 06:50:02       30 阅读