目錄
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;
//任務執行完成,dep等置爲null
dep = null; src = null; fn = null;
return d.postFire(a, mode);
}
}
4、biApplyStage / BiApply
biApplyStage是thenCombine等方法的底層實現,其實現如下:
//當前任務節點和o都執行完了,纔會觸發f的執行
private <U,V> CompletableFuture<V> biApplyStage(
Executor e, CompletionStage<U> o,
BiFunction<? super T,? super U,? extends V> f) {
CompletableFuture<U> b;
if (f == null || (b = o.toCompletableFuture()) == null) //參數異常
throw new NullPointerException();
CompletableFuture<V> d = new CompletableFuture<V>();
//Executor爲null,則通過biApply嘗試執行任務
if (e != null || !d.biApply(this, b, f, null)) {
BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f);
//把c加入到當前CompletableFuture和b的stack鏈表中,當前任務或者b執行完成後都會觸發c的執行
//如果當前任務或者b未執行完成,則不會執行,最後一個執行完成的任務會實際執行c
bipush(b, c);
//嘗試執行
c.tryFire(SYNC);
}
return d;
}
final <R,S> boolean biApply(CompletableFuture<R> a,
CompletableFuture<S> b,
BiFunction<? super R,? super S,? extends T> f,
BiApply<R,S,T> c) {
Object r, s; Throwable x;
//a或者b未執行完成
if (a == null || (r = a.result) == null ||
b == null || (s = b.result) == null || f == null)
return false;
tryComplete: if (result == null) {
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) { //任務a執行異常
completeThrowable(x, r); //保存異常信息
break tryComplete; //終止後面的處理,跳出外層的if分支,返回true
}
r = null; //r就是NIL,將其改成null
}
if (s instanceof AltResult) {
if ((x = ((AltResult)s).ex) != null) { //任務b執行異常
completeThrowable(x, s);
break tryComplete;
}
s = null;
}
try {
//claim返回false,表示會通過線程池異步執行f
if (c != null && !c.claim())
return false;
//由當前線程執行f
@SuppressWarnings("unchecked") R rr = (R) r;
@SuppressWarnings("unchecked") S ss = (S) s;
//保存執行結果
completeValue(f.apply(rr, ss));
} catch (Throwable ex) {
//保存異常信息
completeThrowable(ex);
}
}
return true;
}
@SuppressWarnings("serial")
static final class BiApply<T,U,V> extends BiCompletion<T,U,V> {
BiFunction<? super T,? super U,? extends V> fn;
//dep是當前任務關聯的CompletableFuture,snd是第二個任務,src是最初的任務
BiApply(Executor executor, CompletableFuture<V> dep,
CompletableFuture<T> src, CompletableFuture<U> snd,
BiFunction<? super T,? super U,? extends V> fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<V> tryFire(int mode) {
CompletableFuture<V> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過biApply嘗試執行,返回true表示執行成功,返回false表示不能執行
//如果NESTED調用,則最後一個參數是this,否則是null
!d.biApply(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//任務執行完成,將相關屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
final void bipush(CompletableFuture<?> b, BiCompletion<?,?,?> c) {
if (c != null) {
Object r;
//將c加入到stack鏈表中
while ((r = result) == null && !tryPushStack(c))
lazySetNext(c, null); // clear on failure
if (b != null && b != this && b.result == null) {
//當前任務已執行,則q等於c,否則q等於CoCompletion(c)
Completion q = (r != null) ? c : new CoCompletion(c);
//將其加入到b的stack鏈表中
while (b.result == null && !b.tryPushStack(q))
lazySetNext(q, null); // clear on failure
}
}
}
@SuppressWarnings("serial")
abstract static class BiCompletion<T,U,V> extends UniCompletion<T,V> {
CompletableFuture<U> snd; // second source for action
BiCompletion(Executor executor, CompletableFuture<V> dep,
CompletableFuture<T> src, CompletableFuture<U> snd) {
super(executor, dep, src); this.snd = snd;
}
}
@SuppressWarnings("serial")
static final class CoCompletion extends Completion {
BiCompletion<?,?,?> base;
//base表示待觸發的任務
CoCompletion(BiCompletion<?,?,?> base) { this.base = base; }
final CompletableFuture<?> tryFire(int mode) {
BiCompletion<?,?,?> c; CompletableFuture<?> d;
//base爲null說明已執行,不爲null則嘗試執行c
if ((c = base) == null || (d = c.tryFire(mode)) == null)
return null;
base = null; // detach
return d;
}
//base任務未執行則返回true,否則返回false
final boolean isLive() {
BiCompletion<?,?,?> c;
return (c = base) != null && c.dep != null;
}
}
5、biAcceptStage / BiAccept
biAcceptStage是thenAcceptBoth方法的實現,其調用鏈如下:
//邏輯同biApplyStage
private <U> CompletableFuture<Void> biAcceptStage(
Executor e, CompletionStage<U> o,
BiConsumer<? super T,? super U> f) {
CompletableFuture<U> b;
if (f == null || (b = o.toCompletableFuture()) == null)
throw new NullPointerException();
CompletableFuture<Void> d = new CompletableFuture<Void>();
//Executor爲null,則通過biAccept嘗試執行任務
if (e != null || !d.biAccept(this, b, f, null)) {
BiAccept<T,U> c = new BiAccept<T,U>(e, d, this, b, f);
//將c加入到當前CompletableFuture 和 b的stack鏈表中
bipush(b, c);
c.tryFire(SYNC);
}
return d;
}
//邏輯同biApply
final <R,S> boolean biAccept(CompletableFuture<R> a,
CompletableFuture<S> b,
BiConsumer<? super R,? super S> f,
BiAccept<R,S> c) {
Object r, s; Throwable x;
if (a == null || (r = a.result) == null ||
b == null || (s = b.result) == null || f == null)
return false;
tryComplete: if (result == null) {
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) { //任務a執行異常
completeThrowable(x, r);
break tryComplete;
}
r = null;
}
if (s instanceof AltResult) {
if ((x = ((AltResult)s).ex) != null) { //任務b執行異常
completeThrowable(x, s);
break tryComplete;
}
s = null;
}
try {
//claim返回false,表示會通過線程池異步執行f
if (c != null && !c.claim())
return false;
@SuppressWarnings("unchecked") R rr = (R) r;
@SuppressWarnings("unchecked") S ss = (S) s;
//將執行結果作爲入參,執行f
f.accept(rr, ss);
completeNull(); //設置執行結果爲NIL
} catch (Throwable ex) {
completeThrowable(ex); //保存異常信息
}
}
return true;
}
@SuppressWarnings("serial")
static final class BiAccept<T,U> extends BiCompletion<T,U,Void> {
BiConsumer<? super T,? super U> fn;
BiAccept(Executor executor, CompletableFuture<Void> dep,
CompletableFuture<T> src, CompletableFuture<U> snd,
BiConsumer<? super T,? super U> fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<Void> tryFire(int mode) {
CompletableFuture<Void> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過biAccept嘗試執行,返回true表示執行成功,返回false表示不能執行
!d.biAccept(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//執行完成將各屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
6、biRunStage / BiRun
biRunStage是runAfterBoth方法的實現,其調用鏈如下:
//邏輯同biApplyStage
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
Runnable f) {
CompletableFuture<?> b;
if (f == null || (b = o.toCompletableFuture()) == null)
throw new NullPointerException();
CompletableFuture<Void> d = new CompletableFuture<Void>();
//Executor爲null,則通過biRun嘗試執行任務
if (e != null || !d.biRun(this, b, f, null)) {
BiRun<T,?> c = new BiRun<>(e, d, this, b, f);
bipush(b, c);
c.tryFire(SYNC);
}
return d;
}
//邏輯同biApply
final boolean biRun(CompletableFuture<?> a, CompletableFuture<?> b,
Runnable f, BiRun<?,?> c) {
Object r, s; Throwable x;
if (a == null || (r = a.result) == null ||
b == null || (s = b.result) == null || f == null)
return false;
if (result == null) {
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
completeThrowable(x, r); //任務a執行異常
else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null)
completeThrowable(x, s); //任務b執行異常
else
try {
//claim返回false,表示會通過線程池異步執行f
if (c != null && !c.claim())
return false;
f.run(); //執行f,不要將任務a和b的執行結果作爲入參
completeNull(); //設置執行結果NIL
} catch (Throwable ex) {
completeThrowable(ex); //保存異常
}
}
return true;
}
@SuppressWarnings("serial")
static final class BiRun<T,U> extends BiCompletion<T,U,Void> {
Runnable fn;
BiRun(Executor executor, CompletableFuture<Void> dep,
CompletableFuture<T> src,
CompletableFuture<U> snd,
Runnable fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<Void> tryFire(int mode) {
CompletableFuture<Void> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過biApply嘗試執行,返回true表示執行成功,返回false表示不能執行
!d.biRun(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//執行完成,將相關屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
7、orApplyStage / OrApply
orApplyStage 是 applyToEither方法的底層實現,其調用鏈如下:
其實現跟biApplyStage類似,主要區別在於判斷是否可以執行的條件上,biApplyStage要求兩個任務都執行完成,orApplyStage要求只要有任意一個執行完成即可。
//當前CompletableFuture 和 o有一個執行完成,就會觸發f的執行
private <U extends T,V> CompletableFuture<V> orApplyStage(
Executor e, CompletionStage<U> o,
Function<? super T, ? extends V> f) {
CompletableFuture<U> b;
if (f == null || (b = o.toCompletableFuture()) == null) //參數校驗
throw new NullPointerException();
CompletableFuture<V> d = new CompletableFuture<V>();
//Executor爲null,則通過orApply嘗試執行任務
if (e != null || !d.orApply(this, b, f, null)) {
OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f);
//將c加入到當前CompletableFuture 和b的stack鏈表中,只要有一個執行完成,就會觸發c的執行
orpush(b, c);
//嘗試執行f
c.tryFire(SYNC);
}
return d;
}
final <R,S extends R> boolean orApply(CompletableFuture<R> a,
CompletableFuture<S> b,
Function<? super R, ? extends T> f,
OrApply<R,S,T> c) {
Object r; Throwable x;
if (a == null || b == null ||
//a和b都未執行完成,則返回false
((r = a.result) == null && (r = b.result) == null) || f == null)
return false;
//a或者b有一個執行完了,r爲已執行完的任務的執行結果
tryComplete: if (result == null) { //當前任務未執行完
try {
//claim返回false,表示會通過線程池異步執行f
if (c != null && !c.claim())
return false;
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) {
completeThrowable(x, r); //執行異常,保存異常信息,終止後面的處理,返回true
break tryComplete;
}
r = null; //ex爲null,說明執行結果就是NIL
}
@SuppressWarnings("unchecked") R rr = (R) r;
//將執行結果作爲入參,執行f,並保存f的執行結果
completeValue(f.apply(rr));
} catch (Throwable ex) {
completeThrowable(ex); //保存異常信息
}
}
return true;
}
final void orpush(CompletableFuture<?> b, BiCompletion<?,?,?> c) {
if (c != null) {
while ((b == null || b.result == null) && result == null) {
if (tryPushStack(c)) {
//將c加入到當前CompletableFuture的stack鏈表中
if (b != null && b != this && b.result == null) {
Completion q = new CoCompletion(c);
//將c加入到b的stack鏈表中
while (result == null && b.result == null &&
!b.tryPushStack(q))
lazySetNext(q, null); //tryPushStack返回false,將q的next屬性置爲null
}
break;
}
lazySetNext(c, null); //將c的next屬性置爲null
}
}
}
static final class OrApply<T,U extends T,V> extends BiCompletion<T,U,V> {
Function<? super T,? extends V> fn;
OrApply(Executor executor, CompletableFuture<V> dep,
CompletableFuture<T> src,
CompletableFuture<U> snd,
Function<? super T,? extends V> fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<V> tryFire(int mode) {
CompletableFuture<V> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過orApply嘗試執行,返回true表示執行成功,返回false表示不能執行
!d.orApply(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//已執行完成,將相關屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
8、orAcceptStage / OrAccept
orAcceptStage 是 acceptEither方法的底層實現,其調用鏈如下:
//邏輯同orApplyStage
private <U extends T> CompletableFuture<Void> orAcceptStage(
Executor e, CompletionStage<U> o, Consumer<? super T> f) {
CompletableFuture<U> b;
if (f == null || (b = o.toCompletableFuture()) == null)
throw new NullPointerException();
CompletableFuture<Void> d = new CompletableFuture<Void>();
//Executor爲null,則通過orAccept嘗試執行任務
if (e != null || !d.orAccept(this, b, f, null)) {
OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f);
//將c加入到當前CompletableFuture 和b的stack鏈表中
orpush(b, c);
c.tryFire(SYNC);
}
return d;
}
//邏輯同orApply
final <R,S extends R> boolean orAccept(CompletableFuture<R> a,
CompletableFuture<S> b,
Consumer<? super R> f,
OrAccept<R,S> c) {
Object r; Throwable x;
if (a == null || b == null ||
//a和b都未執行完成,則返回false
((r = a.result) == null && (r = b.result) == null) || f == null)
return false;
tryComplete: if (result == null) {
try {
//claim返回false,表示會通過線程池異步執行f
if (c != null && !c.claim())
return false;
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) {
completeThrowable(x, r);//執行異常
break tryComplete;
}
r = null;
}
//正常執行
@SuppressWarnings("unchecked") R rr = (R) r;
f.accept(rr); //將執行結果作爲入參,調用f,但是不保存f的執行結果
completeNull();
} catch (Throwable ex) {
completeThrowable(ex);
}
}
return true;
}
@SuppressWarnings("serial")
static final class OrAccept<T,U extends T> extends BiCompletion<T,U,Void> {
Consumer<? super T> fn;
OrAccept(Executor executor, CompletableFuture<Void> dep,
CompletableFuture<T> src,
CompletableFuture<U> snd,
Consumer<? super T> fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<Void> tryFire(int mode) {
CompletableFuture<Void> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過orAccept嘗試執行,返回true表示執行成功,返回false表示不能執行
!d.orAccept(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//執行完成,將相關屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
9、orRunStage / OrRun
orRunStage 是 runAfterEither 方法的底層實現,其調用如下:
//邏輯同orApplyStage
private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o,
Runnable f) {
CompletableFuture<?> b;
if (f == null || (b = o.toCompletableFuture()) == null)
throw new NullPointerException();
CompletableFuture<Void> d = new CompletableFuture<Void>();
//Executor爲null,則通過orRun嘗試執行任務
if (e != null || !d.orRun(this, b, f, null)) {
OrRun<T,?> c = new OrRun<>(e, d, this, b, f);
//將c加入到當前CompletableFuture 和b的stack鏈表中
orpush(b, c);
c.tryFire(SYNC);
}
return d;
}
//邏輯同orApply
final boolean orRun(CompletableFuture<?> a, CompletableFuture<?> b,
Runnable f, OrRun<?,?> c) {
Object r; Throwable x;
if (a == null || b == null ||
//a和b都未執行完成,則返回false
((r = a.result) == null && (r = b.result) == null) || f == null)
return false;
if (result == null) {
try {
if (c != null && !c.claim())
return false;
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
completeThrowable(x, r); //執行異常
else { //正常執行完成,不用將執行結果作爲方法入參
f.run();
completeNull();
}
} catch (Throwable ex) {
completeThrowable(ex);
}
}
return true;
}
static final class OrRun<T,U> extends BiCompletion<T,U,Void> {
Runnable fn;
OrRun(Executor executor, CompletableFuture<Void> dep,
CompletableFuture<T> src,
CompletableFuture<U> snd,
Runnable fn) {
super(executor, dep, src, snd); this.fn = fn;
}
final CompletableFuture<Void> tryFire(int mode) {
CompletableFuture<Void> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep不爲null,說明fn未執行,通過orRun嘗試執行,返回true表示執行成功,返回false表示不能執行
!d.orRun(a = src, b = snd, fn, mode > 0 ? null : this))
return null;
//執行完成,各屬性置爲null
dep = null; src = null; snd = null; fn = null;
return d.postFire(a, b, mode);
}
}
10、uniComposeStage / UniRelay
uniComposeStage 是 thenCompose的底層實現,其調用鏈如下:
private <V> CompletableFuture<V> uniComposeStage(
Executor e, Function<? super T, ? extends CompletionStage<V>> f) {
if (f == null) throw new NullPointerException();
Object r; Throwable x;
if (e == null && (r = result) != null) {
//當前CompletableFuture 已執行完成
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) { //執行異常
return new CompletableFuture<V>(encodeThrowable(x, r));
}
r = null;
}
try {
@SuppressWarnings("unchecked") T t = (T) r;
//f執行完成後會返回一個CompletableFuture
CompletableFuture<V> g = f.apply(t).toCompletableFuture();
Object s = g.result;
if (s != null)
//g已執行完成
return new CompletableFuture<V>(encodeRelay(s));
//g未執行
CompletableFuture<V> d = new CompletableFuture<V>();
UniRelay<V> copy = new UniRelay<V>(d, g);
//將copy加入到g的stack鏈表中
g.push(copy);
//嘗試執行copy,如果g未執行完則會立即返回
copy.tryFire(SYNC);
return d;
} catch (Throwable ex) {
return new CompletableFuture<V>(encodeThrowable(ex));
}
}
//當前CompletableFuture 未執行完成
CompletableFuture<V> d = new CompletableFuture<V>();
UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f);
//將c加入到當前CompletableFuture的stack鏈表中
push(c);
c.tryFire(SYNC);
return d;
}
@SuppressWarnings("serial")
static final class UniRelay<T> extends UniCompletion<T,T> { // for Compose
//src是等待執行的任務
UniRelay(CompletableFuture<T> dep, CompletableFuture<T> src) {
super(null, dep, src);
}
final CompletableFuture<T> tryFire(int mode) {
CompletableFuture<T> d; CompletableFuture<T> a;
if ((d = dep) == null || !d.uniRelay(a = src))
return null;
src = null; dep = null;
return d.postFire(a, mode);
}
}
final boolean uniRelay(CompletableFuture<T> a) {
Object r;
if (a == null || (r = a.result) == null) //a未執行完成
return false;
//a已執行完成
if (result == null) //將a的執行結果作爲當前CompletableFuture的執行結果
completeRelay(r);
return true;
}
@SuppressWarnings("serial")
static final class UniCompose<T,V> extends UniCompletion<T,V> {
Function<? super T, ? extends CompletionStage<V>> fn;
//src表示等待執行的任務
UniCompose(Executor executor, CompletableFuture<V> dep,
CompletableFuture<T> src,
Function<? super T, ? extends CompletionStage<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,表示當前任務未執行,則通過uniCompose嘗試執行任務,返回false表示不可執行,返回true表示已執行
!d.uniCompose(a = src, fn, mode > 0 ? null : this))
return null;
//任務執行完成,將相關屬性置爲null
dep = null; src = null; fn = null;
return d.postFire(a, mode);
}
}
final <S> boolean uniCompose(
CompletableFuture<S> a,
Function<? super S, ? extends CompletionStage<T>> f,
UniCompose<S,T> c) {
Object r; Throwable x;
//a未執行完成,則返回false
if (a == null || (r = a.result) == null || f == null)
return false;
tryComplete: if (result == null) {
if (r instanceof AltResult) {
if ((x = ((AltResult)r).ex) != null) {
completeThrowable(x, r); //執行異常,終止處理
break tryComplete;
}
r = null;
}
try {
//claim返回false,表示會異步執行f
if (c != null && !c.claim())
return false;
@SuppressWarnings("unchecked") S s = (S) r;
//將a的執行結果作爲入參,執行f,g表示f的執行結果
CompletableFuture<T> g = f.apply(s).toCompletableFuture();
//如果g的result不爲null,即g已執行,則調用uniRelay,將g的執行結果作爲當前CompletableFuture的執行結果,並返回true
//如果g的result爲null,則uniRelay返回false
if (g.result == null || !uniRelay(g)) {
UniRelay<T> copy = new UniRelay<T>(this, g);
//將copy插入到g的stack鏈表中
g.push(copy);
//嘗試執行
copy.tryFire(SYNC);
if (result == null)
return false;
}
} catch (Throwable ex) {
completeThrowable(ex);
}
}
return true;
}
11、andTree
andTree是allOf方法的底層實現,addTree的多個任務是各自獨立並行執行,addTree將多個任務通過遞歸的方式兩兩組隊,任一一個任務執行完成都會判斷兩兩組隊的兩個任務是否都執行完了,如果是則觸發上層的任務判斷邏輯,直到最終所有任務都執行完了。
//lo是起始數組索引,hi是終止數組索引
static CompletableFuture<Void> andTree(CompletableFuture<?>[] cfs,
int lo, int hi) {
CompletableFuture<Void> d = new CompletableFuture<Void>();
if (lo > hi) //數組爲空
d.result = NIL;
else {
CompletableFuture<?> a, b;
//取中間值
int mid = (lo + hi) >>> 1;
//通過遞歸,將多個任務轉換成兩兩成對的,最底層的執行完了會觸發上層
//這裏的觸發只是任務是否執行完成的判斷邏輯,各任務是各自獨立並行執行
if ((a = (lo == mid ? cfs[lo] :
andTree(cfs, lo, mid))) == null ||
(b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] :
andTree(cfs, mid+1, hi))) == null)
throw new NullPointerException();
//如果a,b有一個未執行完,則返回false
if (!d.biRelay(a, b)) {
BiRelay<?,?> c = new BiRelay<>(d, a, b);
//將c插入到a,b的stack鏈表中,a或者b執行完成後都會觸發c的執行
a.bipush(b, c);
//嘗試執行c
c.tryFire(SYNC);
}
}
return d;
}
boolean biRelay(CompletableFuture<?> a, CompletableFuture<?> b) {
Object r, s; Throwable x;
if (a == null || (r = a.result) == null ||
b == null || (s = b.result) == null)
return false; //a或者b有一個未執行完成,則返回false
if (result == null) {
if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) //a執行異常
completeThrowable(x, r);
else if (s instanceof AltResult && (x = ((AltResult)s).ex) != null) //b執行異常
completeThrowable(x, s);
else
//a,b正常執行
completeNull();
}
return true;
}
static final class BiRelay<T,U> extends BiCompletion<T,U,Void> { // for And
BiRelay(CompletableFuture<Void> dep,
CompletableFuture<T> src,
CompletableFuture<U> snd) {
super(null, dep, src, snd);
}
final CompletableFuture<Void> tryFire(int mode) {
CompletableFuture<Void> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep爲null表示當前任務未執行,則調用biRelay嘗試執行,返回false表示不能執行,返回true表示已執行
!d.biRelay(a = src, b = snd))
return null;
//執行完成,將相關屬性置爲null
src = null; snd = null; dep = null;
return d.postFire(a, b, mode);
}
}
假設有5個任務,則addTree構成的一個遞歸的樹形如下圖:
12、orTree
orTree是anyOf的底層實現,其實現思路和addTree類似,區別在於兩兩成對的任務,只要有一個依賴的任務執行完成了就會觸發器上層任務的執行,直到最上層的任務的執行,其實現如下:
//lo是起始數組索引,hi是終止數組索引
static CompletableFuture<Object> orTree(CompletableFuture<?>[] cfs,
int lo, int hi) {
CompletableFuture<Object> d = new CompletableFuture<Object>();
if (lo <= hi) {
CompletableFuture<?> a, b;
int mid = (lo + hi) >>> 1;
//通過遞歸,將多個任務轉換成兩兩成對的,最底層的執行完了會觸發上層
if ((a = (lo == mid ? cfs[lo] :
orTree(cfs, lo, mid))) == null ||
(b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] :
orTree(cfs, mid+1, hi))) == null)
throw new NullPointerException();
if (!d.orRelay(a, b)) { //返回false,表示不能執行
OrRelay<?,?> c = new OrRelay<>(d, a, b);
//將c插入到a,b的stack鏈表中,a或者b執行完成後都會觸發c的執行
a.orpush(b, c);
c.tryFire(SYNC);
}
}
return d;
}
final boolean orRelay(CompletableFuture<?> a, CompletableFuture<?> b) {
Object r;
if (a == null || b == null ||
((r = a.result) == null && (r = b.result) == null))
return false; //a或者b都未執行,則返回false
if (result == null)
completeRelay(r); //有一個執行完成,將其執行結果作爲當前CompletableFuture的result
return true;
}
@SuppressWarnings("serial")
static final class OrRelay<T,U> extends BiCompletion<T,U,Object> { // for Or
OrRelay(CompletableFuture<Object> dep, CompletableFuture<T> src,
CompletableFuture<U> snd) {
super(null, dep, src, snd);
}
final CompletableFuture<Object> tryFire(int mode) {
CompletableFuture<Object> d;
CompletableFuture<T> a;
CompletableFuture<U> b;
if ((d = dep) == null ||
//dep爲null表示當前任務未執行,則調用biRelay嘗試執行,返回false表示不能執行,返回true表示已執行
!d.orRelay(a = src, b = snd))
return null;
//已執行,相關屬性置爲null
src = null; snd = null; dep = null;
return d.postFire(a, b, mode);
}
}