目录
1、uniWhenCompleteStage / UniWhenComplete
3、uniExceptionallyStage / UniExceptionally
本篇博客继续上一篇《Java8 CompletableFuture(一) 源码解析》讲解CompletableFuture其他方法的实现。
1、uniWhenCompleteStage / UniWhenComplete
uniWhenCompleteStage是whenComplete等方法的底层实现,其调用链如下:
private CompletableFuture<T> uniWhenCompleteStage(
Executor e, BiConsumer<? super T, ? super Throwable> f) {
if (f == null) throw new NullPointerException();
CompletableFuture<T> d = new CompletableFuture<T>();
//e为null,则调用uniWhenComplete尝试执行任务,返回false表示不能执行,返回true表示任务已执行
if (e != null || !d.uniWhenComplete(this, f, null)) {
UniWhenComplete<T> c = new UniWhenComplete<T>(e, d, this, f);
//将c加入到当前任务的Stack链表中
push(c);
//再次尝试执行任务
c.tryFire(SYNC);
}
return d;
}
final boolean uniWhenComplete(CompletableFuture<T> a,
BiConsumer<? super T,? super Throwable> f,
UniWhenComplete<T> c) {
Object r; T t; Throwable x = null;
//如果a未执行完成,返回false
if (a == null || (r = a.result) == null || f == null)
return false;
if (result == null) {
//当前任务未执行
try {
//claim返回false,表示由线程池异步执行当前任务
if (c != null && !c.claim())
return false;
if (r instanceof AltResult) { //执行异常
x = ((AltResult)r).ex;
t = null;
} else {
//正常执行
@SuppressWarnings("unchecked") T tr = (T) r;
t = tr;
}
//将执行结果和异常传递给f
f.accept(t, x);
if (x == null) {
//正常执行,设置执行结果
internalComplete(r);
return true;
}
} catch (Throwable ex) {
if (x == null)
x = ex;
}
//执行异常
completeThrowable(x, r);
}
return true;
}
@SuppressWarnings("serial")
static final class UniWhenComplete<T> extends UniCompletion<T,T> {
BiConsumer<? super T, ? super Throwable> fn;
UniWhenComplete(Executor executor, CompletableFuture<T> dep,
CompletableFuture<T> src,
BiConsumer<? super T, ? super Throwable> fn) {
super(executor, dep, src); this.fn = fn;
}
final CompletableFuture<T> tryFire(int mode) {
CompletableFuture<T> d; CompletableFuture<T> a;
if ((d = dep) == null ||
//dep不为null,表示任务未执行,则尝试执行任务,uniWhenComplete返回false表示不能执行
!d.uniWhenComplete(a = src, fn, mode > 0 ? null : this))
return null;
//uniWhenComplete返回true,任务已执行,将dep等置为null
dep = null; src = null; fn = null;
return d.postFire(a, mode);
}
}
2、uniHandleStage / UniHandle
uniHandleStage是handler等方法的底层实现,其调用链如下:
其实现跟uniWhenCompleteStage类似,区别在于uniHandleStage的回调方法有返回值,uniHandleStage方法创建的CompletableFuture,其result是回调方法的执行结果,而uniWhenCompleteStage创建的CompletableFuture,其result是原始CompletableFuture的执行结果。
private <V> CompletableFuture<V> uniHandleStage(
Executor e, BiFunction<? super T, Throwable, ? extends V> f) {
if (f == null) throw new NullPointerException();
CompletableFuture<V> d = new CompletableFuture<V>();
//e为null,则调用uniWhenComplete尝试执行任务,返回false表示不能执行,返回true表示任务已执行
if (e != null || !d.uniHandle(this, f, null)) {
if (e != null || !d.uniHandle(this, f, null)) {
UniHandle<T,V> c = new UniHandle<T,V>(e, d, this, f);
//将c加入到当前任务的Stack链表中
push(c);
//再次尝试执行任务
c.tryFire(SYNC);
}
return d;
}
final <S> boolean uniHandle(CompletableFuture<S> a,
BiFunction<? super S, Throwable, ? extends T> f,
UniHandle<S,T> c) {
Object r; S s; Throwable x;
//如果a未执行完成,返回false
if (a == null || (r = a.result) == null || f == null)
return false;
if (result == null) { //当前任务未执行
try {
//claim返回false,表示由线程池异步执行当前任务
if (c != null && !c.claim())
return false;
if (r instanceof AltResult) { //执行异常
x = ((AltResult)r).ex;
s = null;
} else { //正常执行
x = null;
@SuppressWarnings("unchecked") S ss = (S) r;
s = ss;
}
//设置执行结果
completeValue(f.apply(s, x));
} catch (Throwable ex) {
//保存异常
completeThrowable(ex);
}
}
return true;
}
@SuppressWarnings("serial")
static final class UniHandle<T,V> extends UniCompletion<T,V> {
BiFunction<? super T, Throwable, ? extends V> fn;
UniHandle(Executor executor, CompletableFuture<V> dep,
CompletableFuture<T> src,
BiFunction<? super T, Throwable, ? extends V> fn) {
super(executor, dep, src); this.fn = fn;
}
final CompletableFuture<V> tryFire(int mode) {
CompletableFuture<V> d; CompletableFuture<T> a;
if ((d = dep) == null ||
//dep不为null,表示任务未执行,则尝试执行任务,uniHandle返回false表示不能执行
!d.uniHandle(a = src, fn, mode > 0 ? null : this))
return null;
//uniHandle返回true,任务已执行,将dep等置为null
dep = null; src = null; fn = null;
return d.postFire(a, mode);
}
}
3、uniExceptionallyStage / UniExceptionally
uniExceptionallyStage是exceptionally方法的实现,其实现如下:
private CompletableFuture<T> uniExceptionallyStage(
Function<Throwable, ? extends T> f) {
if (f == null) throw new NullPointerException();
CompletableFuture<T> d = new CompletableFuture<T>();
//exceptionally方法不能指定Executor
//通过uniExceptionally尝试执行任务,返回false表示不能执行
if (!d.uniExceptionally(this, f, null)) {
UniExceptionally<T> c = new UniExceptionally<T>(d, this, f);
//加入到stack链表中
push(c);
//再次尝试执行
c.tryFire(SYNC);
}
return d;
}
final boolean uniExceptionally(CompletableFuture<T> a,
Function<? super Throwable, ? extends T> f,
UniExceptionally<T> c) {
Object r; Throwable x;
if (a == null || (r = a.result) == null || f == null)
return false;
if (result == null) {
try {
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) {
//如果执行异常
if (c != null && !c.claim()) //因为Executor为null,所以claim肯定返回true
return false;
//将异常作为入参,调用f,并保存执行结果
completeValue(f.apply(x));
} else
//正常执行,将原始任务的执行结果作为当前任务的结果
internalComplete(r);
} catch (Throwable ex) {
//保存异常信息
completeThrowable(ex);
}
}
return true;
}
@SuppressWarnings("serial")
static final class UniExceptionally<T> extends UniCompletion<T,T> {
Function<? super Throwable, ? extends T> fn;
UniExceptionally(CompletableFuture<T> dep, CompletableFuture<T> src,
Function<? super Throwable, ? extends T> fn) {
super(null, dep, src); this.fn = fn;
}
final CompletableFuture<T> tryFire(int mode) { // never ASYNC
// assert mode != ASYNC;
CompletableFuture<T> d; CompletableFuture<T> a;
//dep不为null,表示任务未执行,调用uniExceptionally执行,注意第三个参数固定为this,返回false表示不能执行,
//实际不可能返回false
if ((d = dep) == null || !d.uniExceptionally(a = src, fn, this))
return null;