日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 future.channel().closeFuture().sync()作用 bind(port).sync()作用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、 實例

  • 例子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());}});//前面都是準備階段,執行到bind這行的時候,原生nio代碼里體現的init,register,bind Port才開始真正執行//這里也要進行阻塞sync():保證在初始化完成后才進行操作ChannelFuture future = bootstrap.bind(8888).sync();System.out.println("服務器已啟動。。。");//同步阻塞等待future.channel().closeFuture().sync();} finally {parentGroup.shutdownGracefully();childGroup.shutdownGracefully();}} }

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

    如果我們不想加f.channel().closeFuture().sync()又想保證程序正常運行怎么辦,簡單,去掉finally 里面關閉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());}});//前面都是準備階段,執行到bind這行的時候,原生nio代碼里體現的init,register,bind Port才開始真正執行ChannelFuture future = bootstrap.bind(8888).sync();System.out.println("服務器已啟動。。。");//同步阻塞等待//future.channel().closeFuture().sync();} finally { // parentGroup.shutdownGracefully(); // childGroup.shutdownGracefully();}} }

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

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

    原因是這樣:

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

    總結就是:sync()讓主線程間接調用wait()方法

  • 當然也可以使用監聽器進行異步回調
  • 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());}});//前面都是準備階段,執行到bind這行的時候,原生nio代碼里體現的init,register,bind Port才開始真正執行//這里也要進行阻塞sync():保證在初始化完成后才進行操作ChannelFuture future = bootstrap.bind(8888).sync();future.addListener((ChannelFutureListener) channelFuture -> {if(future.isSuccess()){System.out.println("8888端口綁定成功");System.out.println("服務器已啟動。。。");}else {System.out.println("8888端口綁定失敗");}});} }

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。