日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

future.channel().closeFuture().sync()作用 bind(port).sync()作用

發(fā)布時(shí)間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 future.channel().closeFuture().sync()作用 bind(port).sync()作用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、 實(shí)例

  • 例子1
  • public class NettyServer {public static void main(String[] args) throws InterruptedException {EventLoopGroup parentGroup = new NioEventLoopGroup();EventLoopGroup childGroup = new NioEventLoopGroup();try {ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioServerSocketChannel>() {@Overrideprotected void initChannel(NioServerSocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();//添加處理器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());}});//前面都是準(zhǔn)備階段,執(zhí)行到bind這行的時(shí)候,原生nio代碼里體現(xiàn)的init,register,bind Port才開始真正執(zhí)行//這里也要進(jìn)行阻塞sync():保證在初始化完成后才進(jìn)行操作ChannelFuture future = bootstrap.bind(8888).sync();System.out.println("服務(wù)器已啟動。。。");//同步阻塞等待future.channel().closeFuture().sync();} finally {parentGroup.shutdownGracefully();childGroup.shutdownGracefully();}} }

    在這里面future.channel().closeFuture().sync();這個(gè)語句的主要目的是,如果缺失上述代碼,則main方法所在的線程,即主線程會在執(zhí)行完bind().sync()方法后,會進(jìn)入finally 代碼塊,之前的啟動的nettyserver也會隨之關(guān)閉掉,整個(gè)程序都結(jié)束了。該語句能讓線程進(jìn)入wait狀態(tài),也就是main線程暫時(shí)不會執(zhí)行到finally里面,nettyserver也持續(xù)運(yùn)行,如果監(jiān)聽到關(guān)閉事件,可以優(yōu)雅的關(guān)閉通道和nettyserver,雖然這個(gè)例子中,永遠(yuǎn)不會監(jiān)聽到關(guān)閉事件(代碼比較簡潔,沒有涉及觸發(fā)關(guān)閉事件的代碼)。該例子僅僅為了展示存在api shutdownGracefully,可以優(yōu)雅的關(guān)閉nettyserver。當(dāng)然,如果你在代碼中通過其他線程觸發(fā)了關(guān)閉事件,此時(shí)main會響應(yīng)事件,并進(jìn)入finally代碼塊,進(jìn)行優(yōu)雅的關(guān)閉操作。

    如果我們不想加f.channel().closeFuture().sync()又想保證程序正常運(yùn)行怎么辦,簡單,去掉finally 里面關(guān)閉nettyserver的語句即可。下面我們來改造下:

    public class NettyServer {public static void main(String[] args) throws InterruptedException {EventLoopGroup parentGroup = new NioEventLoopGroup();EventLoopGroup childGroup = new NioEventLoopGroup();try {ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioServerSocketChannel>() {@Overrideprotected void initChannel(NioServerSocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();//添加處理器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());}});//前面都是準(zhǔn)備階段,執(zhí)行到bind這行的時(shí)候,原生nio代碼里體現(xiàn)的init,register,bind Port才開始真正執(zhí)行ChannelFuture future = bootstrap.bind(8888).sync();System.out.println("服務(wù)器已啟動。。。");//同步阻塞等待//future.channel().closeFuture().sync();} finally { // parentGroup.shutdownGracefully(); // childGroup.shutdownGracefully();}} }

    是不是一目了然了?其實(shí)入門例子可以完全按照我的改造的方法來演示,去掉f.channel().closeFuture().sync()。否則,對于剛?cè)腴T的小白來說,無法理解這個(gè)例子中的奧義。

  • 深入研究
  • 前文得知,future.channel().closeFuture().sync()作用是為了讓netty不會關(guān)閉,那么具體是什么原理呢?

    原因是這樣:

    netty為了基于性能,多采用異步方式,而我們通過調(diào)用sync()方法,會讓主線程間接調(diào)用wait()方法,進(jìn)而實(shí)現(xiàn)阻塞的效果。當(dāng)然,在簡單的demo中,會這樣實(shí)現(xiàn)阻塞,但是在正式環(huán)境下,一般不會這么寫。同樣的,在前面“b.bind(port).sync();”中也有sync(),原理類似,當(dāng)調(diào)用bind(port)時(shí),是異步的,因此為了保證在初始化完成后才進(jìn)行操作,避免調(diào)用一個(gè)初始化未完成的句柄,sync方法是等待異步操作執(zhí)行完畢。

    總結(jié)就是:sync()讓主線程間接調(diào)用wait()方法

  • 當(dāng)然也可以使用監(jiān)聽器進(jìn)行異步回調(diào)
  • public class ASychNettyServer {public static void main(String[] args) throws InterruptedException {EventLoopGroup parentGroup = new NioEventLoopGroup();EventLoopGroup childGroup = new NioEventLoopGroup();ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioServerSocketChannel>() {@Overrideprotected void initChannel(NioServerSocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();//添加處理器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());}});//前面都是準(zhǔn)備階段,執(zhí)行到bind這行的時(shí)候,原生nio代碼里體現(xiàn)的init,register,bind Port才開始真正執(zhí)行//這里也要進(jìn)行阻塞sync():保證在初始化完成后才進(jìn)行操作ChannelFuture future = bootstrap.bind(8888).sync();future.addListener((ChannelFutureListener) channelFuture -> {if(future.isSuccess()){System.out.println("8888端口綁定成功");System.out.println("服務(wù)器已啟動。。。");}else {System.out.println("8888端口綁定失敗");}});} }

    總結(jié)

    以上是生活随笔為你收集整理的future.channel().closeFuture().sync()作用 bind(port).sync()作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。