netty源码分析之ChannelFuture

在netty里面所有的nio相关的操作都是异步的,返回一个channelfuture对象,这个里面可以添加一些listener,然后再相关操作完成后进行触发,最主要的是通过这个对象可以查询相关操作的执行情况,是成功了,还是失败了。

         我们来就来看看DefaultChannelFuture的实现就好,我们应该能猜到netty的思想,里面有一个listener的集合,addListener会添加listener到集合里面,然后再真正的io操作完成之后来触发相应的listener,这里面有很多关于多线程的程序,这个还不是很了解,应该后面多恶补恶补。

          

Java代码   收藏代码
  1. private void notifyListeners() {  
  2.         // This method doesn't need synchronization because:  
  3.         // 1) This method is always called after synchronized (this) block.  
  4.         //    Hence any listener list modification happens-before this method.  
  5.         // 2) This method is called only when 'done' is true.  Once 'done'  
  6.         //    becomes true, the listener list is never modified - see add/removeListener()  
  7.         if (firstListener != null) {  
  8.             notifyListener(firstListener);  
  9.             firstListener = null;  
  10.   
  11.             if (otherListeners != null) {  
  12.                 for (ChannelFutureListener l: otherListeners) {  
  13.                     notifyListener(l);  
  14.                 }  
  15.                 otherListeners = null;  
  16.             }  
  17.         }  
  18.     }  

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值