首先我们来看看future和promise接口整体设计
最顶层的future是jdk的,第二个是netty自定义的future,两个同名,继承关系
看看jdk的future接口
public interface Future<V> { // 取消任务 boolean cancel(boolean mayInterruptIfRunning); // 任务是否取消 boolean isCancelled(); // 任务是否完成 boolean isDone(); // 阻塞的获取执行的结果 V get() throws InterruptedException, ExecutionException; // 在一定时间内-超时-阻塞的获取执行的结果 V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
瞅瞅netty的future接口
public interface Future<V> extends java.util.concurrent.Future<V> { // 是否成功 boolean isSuccess(); // 是否取消 boolean isCancellable(); Throwable cause(); // 添加listener进行回调 Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener); Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners); Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener); Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners); // 阻塞的等待任务执行,如果失败则抛出失败原因的异常 Future<V> sync() throws InterruptedException; // 不响应中断等待异常 Future<V> syncUninterruptibly(); // 阻塞等待任务执行,失败不抛异常 Future<V> await() throws InterruptedException; Future<V> awaitUninterruptibly(); boolean await(long timeout, TimeUnit unit) throws InterruptedException; boolean await(long timeoutMillis) throws InterruptedException; boolean awaitUninterruptibly(long timeout, TimeUnit unit); boolean awaitUninterruptibly(long timeoutMillis); // 马上获取到任务的结果,不阻塞,而jdk的future是阻塞的 V getNow(); // 取消任务执行,如果取消成功,任务会因为 CancellationException 异常而导致失败 // 也就是 isSuccess()==false,同时上面的 cause() 方法返回 CancellationException 的实例。 // mayInterruptIfRunning 说的是:是否对正在执行该任务的线程进行中断(这样才能停止该任务的执行), // 似乎 Netty 中 Future 接口的各个实现类,都没有使用这个参数 @Override boolean cancel(boolean mayInterruptIfRunning); }
netty的future在jdk的基础上扩展了它需要的方法,sync和await的区别我们放到下面看实现类的时候说
同时我们也可以看到,这个future接口跟io操作是无关的
接下来我们看看ChannelFuture接口,接口注释上写的很清楚,我们来看看
* The result of an asynchronous {@link Channel} I/O operation.
* <p>
* 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 {@link ChannelFuture} instance which gives you the information about the
* result or status of the I/O operation.
* <p>
* A {@link ChannelFuture} is either <em>uncompleted</em> or <em>completed</em>.
* 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.
所有io操作都是异步的,一个io操作的调用会立即返回一个带有结果或者状态的io实例。
io操作要么是未完成的,要么是完成的。当它开始时,future会被创建,一开始是未完成的,未完成的时候没有成功、失败或者取消状态
当它是完成的时候,可以是失败或者取消的,失败或者取消原因会被附加到future上。
* <pre> * +---------------------------+ * | 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 | * +---------------------------+ * </pre>
上面那个图很清楚了,在两种过程的时候会有什么状态,我们看看接口
public interface ChannelFuture extends Future<Void> { // 返回future关联的channel Channel channel(); // 重写下面几个方法,修改返回值为channelfuture @Override ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener); @Override ChannelFuture addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners); @Override ChannelFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener); @Override ChannelFuture removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners); @Override ChannelFuture sync() throws InterruptedException; @Override ChannelFuture syncUninterruptibly(); @Override ChannelFuture await() throws InterruptedException; @Override ChannelFuture awaitUninterruptibly(); /** * Returns {@code true} if this {@link ChannelFuture} is a void future and so not allow to call any of the * following methods: * <ul> * <li>{@link #addListener(GenericFutureListener)}</li> * <li>{@link #addListeners(GenericFutureListener[])}</li> * <li>{@link #await()}</li> * <li>{@link #await(long, TimeUnit)} ()}</li> * <li>{@link #await(long)} ()}</li> * <li>{@link #awaitUninterruptibly()}</li> * <li>{@link #sync()}</li> * <li>{@link #syncUninterruptibly()}</li> * </ul> 标记该future是void的,使不能使用上面的方法 */ boolean isVoid(); }