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()。否则,对于刚入门的小白来说,无法理解这个例子中的奥义。