jdk8 fork/join 框架探究

直接看代码

join

1
2
3
4
5
6
7
public final V join() {
int s;
if ((s = doJoin() & DONE_MASK) != NORMAL)
// 如果没有正常完成去处理异常
reportException(s);
return getRawResult();
}

doJoin

1
2
3
4
5
6
7
8
9
10
private int doJoin() {
int s; Thread t; ForkJoinWorkerThread wt; ForkJoinPool.WorkQueue w;
// status<0表示任务已经执行完毕
return (s = status) < 0 ? s :
((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
(w = (wt = (ForkJoinWorkerThread)t).workQueue).
tryUnpush(this) && (s = doExec()) < 0 ? s :
wt.pool.awaitJoin(w, this, 0L) :
externalAwaitDone();
}
Read more