Netty_EventLoop_EventLoopGroup

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 cant 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

Netty_EventLoop_EventLoopGroup_第1张图片

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. 

Netty_EventLoop_EventLoopGroup_第2张图片

EventLoop Thread relationship 

The EventLoop is always bound to a single Thread that never changed during its 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 doesnt 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=====

你可能感兴趣的:(Netty_EventLoop_EventLoopGroup)