源码分析-netty-channel-channelFuture

xiaoxiao2021-02-28  92

ChannelFuture 

The result of an asynchronous {@link Channel} I/O operation. 是异步Channel IO的操作结果。

一、异步Channel IO 的2中结果: 1、完成 isDone() = true ,  有3种状态,    (1)成功  isSuccess() = true   (2)失败  , 有失败的异常, isSuccess() = false ,  cause() = is not null   (3)取消 ,  isCancelled() = true 2、未完成   isDone() =  false , isSuccess()= false , isCancelled() = false  二、结果通知谁?谁来处理结果? =》引入 ChannelFutureListener , 基于事件驱动的响应式设计 源码建议:addListener(GenericFutureListener)} is non-blocking.  It simply adds  the specified {@link ChannelFutureListener} to the {@link ChannelFuture}, and  I/O thread will notify the listeners when the I/O operation associated with  the future is done.  {@link ChannelFutureListener} yields the best  performance and resource utilization because it does not block at all, but  it could be tricky to implement a sequential logic if you are not used to  event-driven programming. addListener(GenericFutureListener)}是非阻塞的。 它只是添加   指定的{@link ChannelFutureListener}到{@link ChannelFuture},以及   I / O线程会在与I / O操作相关联时通知侦听器   未来就完成了。 {@link ChannelFutureListener}获得最佳效果   性能和资源利用率,因为它根本不阻止,但是   如果不习惯使用顺序逻辑,可能会很棘手   事件驱动编程。  By contrast, {@link #await()} is a blocking operation.  Once called, the  caller thread blocks until the operation is done.  It is easier to implement   a sequential logic with {@link #await()}, but the caller thread blocks   unnecessarily until the I/O operation is done and there's relatively   expensive cost of inter-thread notification.  Moreover, there's a chance of   dead lock in a particular circumstance, which is described below. 相反,{@link #await()}是一个阻止操作。 一旦调用,   调用程序线程阻塞直到操作完成。 它更容易实现    具有{@link #await()}的顺序逻辑,但调用者线程阻塞    不必要直到I / O操作完成,并有相对    线程间通知成本昂贵。 此外,有机会    在特定情况下死锁,如下所述。  The event handler methods in {@link ChannelHandler} are usually called by  an I/O thread.  If {@link #await()} is called by an event handler   method, which is called by the I/O thread, the I/O operation it is waiting  for might never complete because {@link #await()} can block the I/O  operation it is waiting for, which is a dead lock. {@link ChannelHandler}中的事件处理程序方法通常由I / O线程调用。 如果{@link #await()}被事件处理程序调用    方法,由I / O线程调用,它等待的I / O操作   因为{@link #await()}可以阻止I / O,因此可能永远不会完成   操作正在等待,这是一个死锁。 三、 ChannelFuture 的 接口定义与实现     列举几个重要的 方法,添加和移除listener, 转同步结果,等待  1、Channel  channel();  哪个 Channel 的 处理结果。 2、ChannelFuture addListener(); 3、ChannelFuture remove(); 4、ChannelFuture  sync(); 5、ChannelFuture  await(); 以下简单介绍几个重要类的实现  1、CompleteChannelFuture  extends CompleteChannel implements ChannelFuture        (1)  addListener() ; 添加一个 监听类,做了哪些事?    立刻告诉监听者,future has completed ,  促发 事件完成后需要 干的活。回调 监听者的函数      (2)完成的ChannelFuture 有两个 更具体的实现          SuccessChannelFuture  和 FailedChannelFuture          2、ChannelProgressiveFuture extends ChannelFuture,ProgressiveFuture    源码简介: An special { @link ChannelFuture} which is used to indicate the { @link FileRegion} transfer progress 一个特殊的{@link ChannelFuture},用于指示{@link FileRegion}传输进度 ChannelProgressivePromise extends ProgressivePrimise , ChannelProgressiveFuture , ChannelPromise 源码简介: Special { @link ChannelPromise} which will be notified once the associated bytes is transferring 特殊的{@link ChannelPromise},一旦相关的字节传输,它将被通知。

四、产生 ChannelFuture 的源头  channel 的 IO 事件  1、Channel 接口的定义,  一个能够进行I / O操作(如读,写,连接和绑定)的组件, 源码简介: (1)ChannelConfig  保存 Channel 的所有的配置属性 (2)ChannelPipeline 处理 Channel 的所有的 IO事件 和请求,  (3)所有的 IO 操作都是异步的,Netty中的所有I / O操作都是异步的。 这意味着任何I / O调用将立即返回,不保证所请求的I / O操作    在通话结束时完成。 相反,你会被带回来一个{@link ChannelFuture}实例,当您出现I / O操作成功,失败或取消时会通知您。 (4)Channel 是分成的,  
转载请注明原文地址: https://www.6miu.com/read-52533.html

最新回复(0)