问题描述
这是java.util.concurrent.CountedCompleter
类(JDK 1.8.0_25)的代码片段.
Here is a code fragment of java.util.concurrent.CountedCompleter
class (JDK 1.8.0_25).
/**
* If the pending count is nonzero, decrements the count;
* otherwise invokes {@link #onCompletion(CountedCompleter)}
* and then similarly tries to complete this task's completer,
* if one exists, else marks this task as complete.
*/
public final void tryComplete() {
CountedCompleter<?> a = this, s = a;
for (int c;;) {
if ((c = a.pending) == 0) {
a.onCompletion(s);
if ((a = (s = a).completer) == null) {
s.quietlyComplete();
return;
}
}
else if (U.compareAndSwapInt(a, PENDING, c, c - 1))
return;
}
}
这让我真的很困惑.该文档说:然后类似地尝试完成此任务的完成程序",但是我看不到对此任务的完成程序进行任何"complete"调用;或其他任何调用.
It makes me really confused. The documentation says: "and then similarly tries to complete this task's completer", but I do not see any invocations of 'complete' on this task's completer; or any other calls to it.
有人在这堂课上工作吗?文档或实施是否有问题?我也可能以错误的方式烹饪.任何对如何正确处理此类的想法都表示赞赏.
Have anybody worked with this class? Is it an issue with documentation or implementation? I might also cook it in a wrong way. Any ideas how to properly deal with this class is appreciated.
推荐答案
您感到困惑吗?每个人都很困惑.我已经对F/J框架进行了四年的批评,我可以告诉您,复杂性水平已经达到8u40的临界水平.此类之所以存在,是因为join()不起作用 .为了避开Java8流的停滞线程,架构师发明了此类.
You're confused? Everyone is confused. I've been writing a critique on the F/J framework for four years now and I can tell you the level of complexity is reaching the critical level with 8u40. The reason this class exists at all is because the join() doesn't work. In order to get around the stalling threads for Java8 streams the architect invented this class.
使用此类的方法是为每个fork()添加addToPendingCount().在compute()中,完成后,您尝试tryComplete().当计数为零时,该方法将调用您的onCompletion().有点混乱,但是如果您的代码很简单,它就可以工作.
The way you work this class is you addToPendingCount() for every fork(). In the compute(), when done, you tryComplete(). When the count is zero, the method calls your onCompletion(). A bit messy but if your code is simple, it works.
其余的代码用于当前CountedCompleter具有自己的CountedCompleter对象链的情况.我的猜测是,这可能是针对parallel.stream处理的.
The rest of the code you see is for when the current CountedCompleter has a chain of CountedCompleter objects of its own. My guess is this is probably for the parallel.stream processing.
这篇关于由docs和CountedCompleter的来源混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!