netty源码分析之Future/Promise

来源:互联网 发布:知乎油腻冯唐 编辑:程序博客网 时间:2024/05/16 17:03

一、前言

在分析Netty的源码的时候,一直看到Future、Promise之类的类,Future之前还接触过一点
知道大致是什么东西,不过Promise没了解过,所以在分析源码之前,顺便学习下Future/Promise~

二、Future

1、Future是什么?

首先需要明白一点的是,它不是netty自己私有的东西,它在jdk1.5后的concurrent包中就已经有
了.
通过一个例子了解本质:
1、client向server提交任务
2、server端开启一个线程去处理,但是也马上返回一个Future给client
3、client可能此时做一些其它操作
4、client获取Future对象,调用Future的方法(比方说get())来获取真正数据

代码例子参考:http://www.cnblogs.com/wade-luffy/p/6229410.html

原生jdk中Future类方法

由图可以看出,原生jdk提供的方法比较少.我们通过get()方法来获取异步执行的完成状态,如果执行完成返回结果,否则一直等待.但是这个方法有一个弊端,就是如果任务一直没完成,那就会处于一直等待的状态,即使有V get(long timeout, TimeUnit unit)这个函数,
如果超过了超时时间也会报超长,netty提供了更好的方法.

下面看下netty中的:

我们需要知道的是在netty每个操作都是异步的,(应该也不能这么说吧,应该说成非阻塞,因为底层是基于nio,aio才是完全异步的).

netty中Future类方法

它继承了原生的Future,扩充了方法,比方说:addListener(GenericFutureListener

2、ChannelFuture

ChannelFuture是netty中异步Future,作用是用来保存Channel异步操作的结果,这个实例将给你一些关于I/O操作结果或者状态的信息。

netty中ChannelFuture类方法

注意:调用await系列方法指定的等待超时时间和I/O超时时间之间是没有特定关系的。如果没有设置I/O超时时间,那么可能在await方法超时之后,future方法其实是没有执行完的。

2、Promise

    /**     * Special {@link Future} which is writable.     */    public interface Promise<V> extends Future<V> {        /**         * Marks this future as a success and notifies all         * listeners.         *         * If it is success or failed already it will throw an {@link IllegalStateException}.         */        Promise<V> setSuccess(V result);        /**         * Marks this future as a success and notifies all         * listeners.         *         * @return {@code true} if and only if successfully marked this future as         *         a success. Otherwise {@code false} because this future is         *         already marked as either a success or a failure.         */        boolean trySuccess(V result);        /**         * Marks this future as a failure and notifies all         * listeners.         *         * If it is success or failed already it will throw an {@link IllegalStateException}.         */        Promise<V> setFailure(Throwable cause);        /**         * Marks this future as a failure and notifies all         * listeners.         *         * @return {@code true} if and only if successfully marked this future as         *         a failure. Otherwise {@code false} because this future is         *         already marked as either a success or a failure.         */        boolean tryFailure(Throwable cause);        /**         * Make this future impossible to cancel.         *         * @return {@code true} if and only if successfully marked this future as uncancellable or it is already done         *         without being cancelled.  {@code false} if this future has been cancelled already.         */        boolean setUncancellable();        @Override        Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);        @Override        Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);        @Override        Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);        @Override        Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);        @Override        Promise<V> await() throws InterruptedException;        @Override        Promise<V> awaitUninterruptibly();        @Override        Promise<V> sync() throws InterruptedException;        @Override        Promise<V> syncUninterruptibly();    }

从上面可以看出其实Promise就是一个可写的Future,没有我想象的那么复杂.其实如果是理解了Future,那么Promise其实也是能够理解了.从它继承Future
和添加的几个借口方法也能看出来它的大致用途.

三、总结

ok ,知道了什么是Future和Promise,接下来就可以开始netty源码之旅了~~~

参考博客:http://www.cnblogs.com/winkey4986/p/6203225.html

原创粉丝点击