netty中future.channel().closeFuture().sync()作用

看到的非常通俗的讲解,记录一下

 原文链接:netty中future.channel().closeFuture().sync()作用_大米饭-CSDN博客

future.channel().closeFuture().sync() 执行完 会使主线程 wait 阻塞后续逻辑执行


我们看下官方的入门例子:

启动一个nettyserver,监听8088端口,再通过命令行执行 telnet localhost 8088来连接。
完整的例子请参考之前的入门例子介绍:
Netty入门官方例子解析(一)丢弃服务器《入门例子,不返回消息》章节

public class DiscardServer {       
        try {                       
            f = b.bind(port).sync();
            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            f.channel().close();
        }
    }   
}


在这里面future.channel().closeFuture().sync();这个语句的主要目的是,如果缺失上述代码,则main方法所在的线程,即主线程会在执行完bind().sync()方法后,会进入finally 代码块,之前的启动的nettyserver也会随之关闭掉,整个程序都结束了。原文的例子有英文注释:
Wait until the server socket is closed,In this example, this does not happen, but you can do that to gracefully shut down your server.
让线程进入wait状态,也就是main线程暂时不会执行到finally里面,nettyserver也持续运行,如果监听到关闭事件,可以优雅的关闭通道和nettyserver,虽然这个例子中,永远不会监听到关闭事件。也就是说这个例子是仅仅为了展示存在api shutdownGracefully,可以优雅的关闭nettyserver。

下面我们来验证下:

public class DiscardServer {
       
        try {
                       
            f = b.bind(port).sync();
          //  f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            f.channel().close();
        }
    }   
}


我们注掉了future.channel().closeFuture().sync(),这时,再执行telnet命令的时候会提示连接失败


如果我们不想加f.channel().closeFuture().sync()又想保证程序正常运行怎么办,简单,去掉finally 里面关闭nettyserver的语句即可。下面我们来改造下: 

 try {
                       
            f = b.bind(port).sync();
          //  f.channel().closeFuture().sync();
        } finally {
            // 资源优雅释放
            //bossGroup.shutdownGracefully();
            //workerGroup.shutdownGracefully();
            //f.channel().close();
        }


是不是一目了然了?其实入门例子可以完全按照我的改造的方法来演示,去掉f.channel().closeFuture().sync()。否则,对于刚入门的小白来说,无法理解这个例子中的奥义。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值