Netty_EventLoop_EventLoopGroup

本文详细解释了Netty中所有I/O操作都是异步执行的机制,包括连接主机、发送消息等操作。重点介绍了Future和ChannelFuture的概念,以及如何使用它们来注册监听器,以获取操作成功或失败的通知。同时,文章阐述了EventLoop和EventLoopGroup之间的关系,以及EventLoopThread如何确保Netty事件处理无需同步。最后,通过图3.1展示了设计如何确保无需手动同步处理Netty事件。

All IO operations in Netty are performed asynchronously. 


Future和ChannelFuture

So when you connect to a host for example,  this  is  done  asynchronously(异步的)  by  default.  The  same  is  true when  you  write/send  a message.  This  means  the operation  may  not  be  performed  directly  but picked up  later  for execution. Because of this you can t know if an operation was successful or not after it returns, but need to be able to check later for success or have some kind of ways to register a listener which is notified. To rectify(纠正) this, Netty uses Futures and ChannelFutures. This future can be used  to  register  a  listener,  which  will  be  notified  when  an  operation  has  either  failed  or completed successfully. 


Netty EventLoop和EventLoopGroup

102227_1NL9_1469576.png

The  relationship  between  an  EventLoop and  an EventLoopGroup may  not  be immediately(立即马上)  intuitive(直观的),  because  we have  said  that  an EventLoopGroup contains  one or  more EventLoop but  this  diagram  shows  that  in  fact,  an  EventLoop passes  the   is-a EventLoopGroup test,  i.e.  an EventLoop is  an EventLoopGroup.   This means  wherever you can pass in an EventLoopGroup you can also just use a specific(具体的) EventLoop. 


figure  3.1  shows  why  the  design  Netty  uses  to  ensure  no  synchronization(同步)  is required on your part to process Netty events. 

103741_L46w_1469576.png

EventLoop Thread relationship 

The EventLoop is always bound to a single Thread that never changed during it s life time. 

When a channel is registered, Netty binds that channel to a single EventLoop(and so to a single thread) for the life time of that Channel. This is why your application doesn t need to synchronize  on  Netty  IO  operations  because  all  IO  for  a  given Channel will  always  be performed by the same thread. 

all  IO  for  a  given Channel will  always  be performed by the same thread. 

=====END=====

转载于:https://my.oschina.net/xinxingegeya/blog/285831

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值