java - Netty的future.channel().closeFuture().sync();到底有什么用?

 x-诗儿_683 发布于 2022-10-29 09:14

我看到很多Netty的例子都在末尾加上了这句话:future.channel().closeFuture().sync();

比如:

public class TimeServer {
    private int count = 0;

    public void bind(int port) {
        try {
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workGroup = new NioEventLoopGroup();
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) // (3)
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel arg0) throws Exception {
                            arg0.pipeline().addLast(new LineBasedFrameDecoder(1024));
                            arg0.pipeline().addLast(new StringDecoder());
                            arg0.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    // ByteBuf buf = (ByteBuf) msg;
                                    // byte[] req = new
                                    // byte[buf.readableBytes()];
                                    // buf.readBytes(req);
                                    // String body = new String(req, "UTF-8");
                                    System.out.println(
                                            "The Time Server  Received order:" + msg + "; the  counter is:" + ++count);

                                    // String currentTime = "QUERY TIME
                                    // ORDER".equalsIgnoreCase(body)
                                    // ? new
                                    // Date(System.currentTimeMillis()).toString()
                                    // : "BAD ORDER";
                                    //
                                    // currentTime = currentTime +
                                    // System.getProperty("line.separator");
                                    // ByteBuf resp =
                                    // Unpooled.copiedBuffer(currentTime.getBytes());
                                    // ctx.writeAndFlush(resp);
                                }
                            });
                        }
                    });
            ChannelFuture future = b.bind(port).sync();

            System.out.println("Server start listen at " + port);
            future.channel().closeFuture().sync();
            
            System.out.println("执行到这里 " + port);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TimeServer().bind(10000);

    }
}

但是我看这行代码一直没有执行。请问这是怎么回事呢?

1 个回答
  • 不是没执行,是主线程到这里就 wait 子线程退出了,子线程才是真正监听和接受请求的。

    2022-11-12 01:46 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有