Netty源码解读DefaultPromise
来源:互联网 发布:ds1302数据手册 编辑:程序博客网 时间:2024/06/05 12:49
这个类是做什么的
这个类是用来获取结果信息的,就是用来获取线程执行的结果信息,但是在获取结果信息的时候,还可以添加一些监听的信息。
该类的继承结构
该类是netty底层源码最基础的类,其他的类都是依赖该类的实现,结构为
java中的Future->Future>AbstractFuture->DefaultPromise
内部有一个 volatile Object result;对象,用来表示设置该Profise为成功的标识,其中volatile关键字,是为了表示各个线程在主内存的可见性
方法分析
1.setSuccess方法
@Override public Promise<V> setSuccess(V result) { if (setSuccess0(result)) { notifyListeners(); return this; } throw new IllegalStateException("complete already: " + this); } private boolean setSuccess0(V result) { if (isDone()) { return false; } synchronized (this) { // Allow only once. if (isDone()) { return false; } if (result == null) { this.result = SUCCESS; } else { this.result = result; } if (hasWaiters()) { notifyAll(); } } return true; }
该方法是为了标识该Promise已经成功获取到结果,其中result是一个成功的标识,看该方法的流程,首先判断isDone,判断该流程是否已经完成,isdone方法也是判断的是result != null && result != UNCANCELLABLE; 就是通过该值来判断该Promise是否已经成功。后续通过synchronized进行线程资源的锁定,进入到synchronized内部,又一次的双锁的判定isdone,最后有一个重点,notifyAll()方法,在该类的内部有一个private short waiters;的变量,用来表示目前该线程的阻塞等待数目,如果此时waiter大于o的话,此时设置了该promise为成功的话,那么直接唤醒所有等待的线程,一旦设置成功后,后续的notifyListeners()是为了通知放置在该Future上的监听
2.isCancelled、isCancellable、isDone
这些方法就好理解了,就是通过判断result标识来判断是否可以取消等操作
3.DefaultFutureListeners、GenericFutureListener
其中DefaultFutureListeners是GenericFutureListener的一个集合,看下面的代码就可以知道,都是在Future上添加监听操作
final class DefaultFutureListeners { private GenericFutureListener<? extends Future<?>>[] listeners; private int size; private int progressiveSize; // the number of progressive listeners }
在DefaultPromise内部有一个private Object listeners;对象,就是用来保持对监听的引用,还有一个lateListeners,这个监听是一个队列,用来保持防止监听的顺序引用,当Future的isdone的时候,顺序的执行监听
@Override public Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener) { if (listener == null) { throw new NullPointerException("listener"); } if (isDone()) { notifyLateListener(listener); return this; } synchronized (this) { if (!isDone()) { if (listeners == null) { listeners = listener; } else { if (listeners instanceof DefaultFutureListeners) { ((DefaultFutureListeners) listeners).add(listener); } else { final GenericFutureListener<? extends Future<V>> firstListener = (GenericFutureListener<? extends Future<V>>) listeners; listeners = new DefaultFutureListeners(firstListener, listener); } } return this; } } notifyLateListener(listener); return this; }
上面是添加监听的代码,首先判断是否已经完成,如果完成直接调用监听代码,否则的话,进行listener判断,其中DefaultFutureListeners 是GenericFutureListener的一个组装集合,后续的removeListeners、removeListener代码类似
4.sync同步阻塞方法
该方法是同步阻塞方法,阻塞一直到获取到结果,看一下该方法的实现
@Override public Promise<V> sync() throws InterruptedException { await(); rethrowIfFailed(); return this; } @Override public Promise<V> await() throws InterruptedException { if (isDone()) { return this; } if (Thread.interrupted()) { throw new InterruptedException(toString()); } synchronized (this) { while (!isDone()) { checkDeadLock(); incWaiters(); try { wait(); } finally { decWaiters(); } } } return this; }
方法首先就是调用了await阻塞,在该方法内部,还是忽略了成功的情况,然后进行线程中断标记的判断。
补充一下线程的基础知识
interrupt()的作用是中断本线程。
本线程中断自己是被允许的;其它线程调用本线程的interrupt()方法时,会通过checkAccess()检查权限。这有可能抛出SecurityException异常。
如果本线程是处于阻塞状态:调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态,或者调用线程的join(), join(long), join(long, int), sleep(long), sleep(long, int)也会让它进入阻塞状态。若线程在阻塞状态时,调用了它的interrupt()方法,那么它的“中断状态”会被清除并且会收到一个InterruptedException异常。例如,线程通过wait()进入阻塞状态,此时通过interrupt()中断该线程;调用interrupt()会立即将线程的中断标记设为“true”,但是由于线程处于阻塞状态,所以该“中断标记”会立即被清除为“false”,同时,会产生一个InterruptedException的异常。
如果线程被阻塞在一个Selector选择器中,那么通过interrupt()中断它时;线程的中断标记会被设置为true,并且它会立即从选择操作中返回。
如果不属于前面所说的情况,那么通过interrupt()中断线程时,它的中断标记会被设置为“true”。
中断一个“已终止的线程”不会产生任何操作。
然后后面就是对该线程上执行的操作,对waiters进行+1,执行完后,又做减操作,这个变量的作用,上述也提到过。await方法内部,就是一个阻塞时间的判断
5.syncUninterruptibly方法
与上面的方法类似,就是缺少了,线程中断标志的判断
- Netty源码解读DefaultPromise
- Netty源码解读
- Netty源码解读Promise
- Netty源码解读EventExecutorGroup
- Netty源码解读 Netty中的buffer
- Netty源码解读之线程
- Netty源码解读之线程
- Netty中的Future源码解读
- java.lang.ClassNotFoundException: io.netty.util.concurrent.DefaultPromise$2
- Netty源码解读(一)概述
- Netty源码解读(一)概述
- Netty源码解读 Channel与Pipeline
- Netty源码解读(一)概述
- Netty源码解读(一)概述
- Netty源码解读------------ServerBootstrap的启动(一)
- Netty源码解读(一)概述
- Netty源码解读------------客户端接入绑定(二)
- Netty源码解读(一)概述
- C/C++ key concepts (1)
- Codeforces Round #345 (Div. 2) D. Image Preview 前缀和
- 分享centos+python+keras+tensorflow环境的docker镜像
- 王者荣耀之我的思考
- Linux tomcat jvm 远程监控
- Netty源码解读DefaultPromise
- linux opencv 安装
- JAVA的Println源码
- 解决Windows系统下git文件名大小写无法修改的问题
- Android greenDao3.0的开发实战,包括版本升级(二)
- Initialization and assignment in C++
- 大学物理实验报告 迈克尔逊干涉 and 三棱镜衍射
- SpringMVC报错找不到ContextLoaderListener 的解决
- 苏嵌指针