netty底层是事件驱动的异步库 但是可以await或者sync(本质是future超时机制)同步返回 但是官方 Prefer addListener(GenericFutureListener) to await()
Interface ChannelFuture
- All Superinterfaces:
- java.util.concurrent.Future<java.lang.Void>
- All Known Subinterfaces:
- ChannelProgressiveFuture, ChannelProgressivePromise, ChannelPromise
- All Known Implementing Classes:
- DefaultChannelProgressivePromise, DefaultChannelPromise
public interface ChannelFuture
extends Future<java.lang.Void>The result of an asynchronousChannelI/O operation.All I/O operations in Netty are asynchronous. It means any I/O calls will return immediately with no guarantee that the requested I/O operation has been completed at the end of the call. Instead, you will be returned with a
ChannelFutureinstance which gives you the information about the result or status of the I/O operation.A
ChannelFutureis either uncompleted or completed. When an I/O operation begins, a new future object is created. The new future is uncompleted initially - it is neither succeeded, failed, nor cancelled because the I/O operation is not finished yet. If the I/O operation is finished either successfully, with failure, or by cancellation, the future is marked as completed with more specific information, such as the cause of the failure. Please note that even failure and cancellation belong to the completed state.+---------------------------+
| Completed successfully |
+---------------------------+
+----> isDone() = true |
+--------------------------+ | | isSuccess() = true |
| Uncompleted | | +===========================+
+--------------------------+ | | Completed with failure |
| isDone() = false | | +---------------------------+
| isSuccess() = false |----+----> isDone() = true |
| isCancelled() = false | | | cause() = non-null |
| cause() = null | | +===========================+
+--------------------------+ | | Completed by cancellation |
| +---------------------------+
+----> isDone() = true |
| isCancelled() = true |
+---------------------------+Various methods are provided to let you check if the I/O operation has been completed, wait for the completion, and retrieve the result of the I/O operation. It also allows you to add
ChannelFutureListeners so you can get notified when the I/O operation is completed.Prefer
addListener(GenericFutureListener)toawait()It is recommended to prefer
addListener(GenericFutureListener)toawait()wherever possible to get notified when an I/O operation is done and to do any follow-up tasks.addListener(GenericFutureListener)is non-blocking. It simply adds the specifiedChannelFutureListenerto theChannelFuture, and I/O thread will notify the listeners when the I/O operation associated with the future is done.ChannelFutureListeneryields 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.By contrast,
await()is a blocking operation. Once called, the caller thread blocks until the operation is done. It is easier to implement a sequential logic withawait(), 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.Do not call
await()insideChannelHandlerThe event handler methods in
ChannelHandlerare usually called by an I/O thread. Ifawait()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 becauseawait()can block the I/O operation it is waiting for, which is a dead lock.// BAD - NEVER DO THIS
@Override
public void channelRead(ChannelHandlerContextctx, Object msg) {
ChannelFuturefuture = ctx.channel().close();
future.awaitUninterruptibly();
// Perform post-closure operation
// ...
} // GOOD
@Override
public void channelRead(ChannelHandlerContextctx, Object msg) {
ChannelFuturefuture = ctx.channel().close();
future.addListener(newChannelFutureListener() {
public void operationComplete(ChannelFuturefuture) {
// Perform post-closure operation
// ...
}
});
}In spite of the disadvantages mentioned above, there are certainly the cases where it is more convenient to call
await(). In such a case, please make sure you do not callawait()in an I/O thread. Otherwise,BlockingOperationExceptionwill be raised to prevent a dead lock.Do not confuse I/O timeout and await timeout
The timeout value you specify with
Future.await(long),Future.await(long, TimeUnit),Future.awaitUninterruptibly(long), orFuture.awaitUninterruptibly(long, TimeUnit)are not related with I/O timeout at all. If an I/O operation times out, the future will be marked as 'completed with failure,' as depicted in the diagram above. For example, connect timeout should be configured via a transport-specific option:// BAD - NEVER DO THIS
Bootstrapb = ...;
ChannelFuturef = b.connect(...);
f.awaitUninterruptibly(10, TimeUnit.SECONDS);
if (f.isCancelled()) {
// Connection attempt cancelled by user
} else if (!f.isSuccess()) {
// You might get a NullPointerException here because the future
// might not be completed yet.
f.cause().printStackTrace();
} else {
// Connection established successfully
} // GOOD
Bootstrapb = ...;
// Configure the connect timeout option.
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
ChannelFuturef = b.connect(...);
f.awaitUninterruptibly(); // Now we are sure the future is completed.
assert f.isDone(); if (f.isCancelled()) {
// Connection attempt cancelled by user
} else if (!f.isSuccess()) {
f.cause().printStackTrace();
} else {
// Connection established successfully
}
netty底层是事件驱动的异步库 但是可以await或者sync(本质是future超时机制)同步返回 但是官方 Prefer addListener(GenericFutureListener) to await()的更多相关文章
- ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用!因此,ES transport client可以同步调用也可以异步(不过底层的socket必然是异步实现)
ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用! 因此,ES tra ...
- twisted是python实现的基于事件驱动的异步网络通信构架。
网:https://twistedmatrix.com/trac/ http://www.cnblogs.com/wy-wangyan/p/5252271.html What is Twisted? ...
- netty源码分析(十八)Netty底层架构系统总结与应用实践
一个EventLoopGroup当中会包含一个或多个EventLoop. 一个EventLoop在它的整个生命周期当中都只会与唯一一个Thread进行绑定. 所有由EventLoop所处理的各种I/O ...
- python2.0_s12_day9_事件驱动编程&异步IO
论事件驱动与异步IO 事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的处理.另外两种常见的编程范式是(单线程)同步以及 ...
- 事件驱动与异步IO--待更新
论事件驱动与异步IO 通常,我们写服务器处理模型的程序时,有以下几种模型: (1)每收到一个请求,创建一个新的进程,来处理该请求: (2)每收到一个请求,创建一个新的线程,来处理该请求: (3)每收到 ...
- python异步库
https://github.com/aio-libs 异步库
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...
- Netty源码分析第7章(编码器和写数据)---->第5节: Future和Promies
Netty源码分析第七章: 编码器和写数据 第五节: Future和Promise Netty中的Future, 其实类似于jdk的Future, 用于异步获取执行结果 Promise则相当于一个被观 ...
- Oracle数据库由dataguard备库引起的log file sync等待
导读: 最近数据库经常出现会话阻塞的报警,过一会又会自动消失,昨天晚上恰好发生了一次,于是赶紧进行了查看,不看不知道,一看吓一跳,发现是由dataguard引起的log file sync等待.我们知 ...
随机推荐
- XCode下Swift – WebView IOS demo
简介 我今天用Mac升级了XCode到8.1,Swift版本应该到了swift3,按网上的demo写webview的例子,报一堆错,整了一天才搞定,不想其他人踩坑了! XCode8.1 ,swift3 ...
- nyoj--118--修路方案(次小生成树)
修路方案 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 南将军率领着许多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N,由于交通不太便利,南将军准备修路. ...
- Windows系统安装MySQL5.5.21图解教程
大家都知道MySQL是一款中.小型关系型数据库管理系统,很具有实用性,对于我们学习很多技术都有帮助 数据库是5.5.21这个版本的.以下是安装步骤: 1.首先单击MySQL5.5.21的安装文件,出现 ...
- JS应用实例1:表格各行换色
效果如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Android 蓝牙4.0的连接和通讯
1.加入权限 <uses-sdk android:minSdkVersion=" android:targetSdkVersion="/> <uses-featu ...
- QQ自动登录里的一些控件知识
在这个程序里面有个读取计算机指定文件的知识: private void button2_Click(object sender, EventArgs e) { openFileDialog1.Show ...
- JavaScript 获取某个字符的 Unicode 码
function getUnicode (charCode) { return charCode.charCodeAt(0).toString(16); } 获取的是 UTF-16 编码的值,不足4位 ...
- net-speeder 安装
net-speeder net-speeder 在高延迟不稳定链路上优化单线程下载速度 项目由https://code.google.com/p/net-speeder/ 迁入 A program t ...
- python 一句话输出26个英文字母
chr(i) # return i character ord(c) # return integer >>> [chr(i) for i in range(97,123)] ['a ...
- 解决从Excel导入数据库,导入到DataTable时数据类型发生变化的问题(如数字类型变成科学计数法,百分数变成小数)
做项目的时候,C#读取Excel数据到DataTable或者DataSet,设断点查看DataTable,发现Excel的显示为较长位数数字的字段如0.000012在DataTable中显示为科学计数 ...