java8 anymatch,Java 8并行流+ anyMatch - 一旦发现匹配,线程是否会被中断?

If I have a parallel stream in java 8, and I terminate with an anyMatch, and my collection has an element that matches the predicate, I'm trying to figure out what happens when one thread processes this element.

I know that anyMatch is short circuiting, so that I wouldn't expect further elements to be processed once the matching element is processed. My confusion is about what happens to the other threads, that are presumably in the middle of processing elements. I can think of 3 plausible scenarios:

a) Do they get interrupted?

b) Do they keep processing the element that they are working on, and then, once all the threads are doing nothing, I get my result?

c) Do I get my result, but the threads that were processing other elements continue processing those elements (but don't take on other elements once they are done)?

I have a long running predicate, where it is very useful to terminate quickly as soon as I know that one element matches. I worry a bit since I can't find this information in the documentation that it might be an implementation dependent thing, which would also be good to know.

Thanks

解决方案

After some digging through the Java source code I think I found the answer.

The other threads periodically check to see if another thread has found the answer and if so, then they stop working and cancel any not yet running nodes.

java.util.Stream.FindOps$FindTask has this method:

private void foundResult(O answer) {

if (isLeftmostNode())

shortCircuit(answer);

else

cancelLaterNodes();

}

Its parent class, AbstractShortcircuitTask implements shortCircuit like this:

/**

* Declares that a globally valid result has been found. If another task has

* not already found the answer, the result is installed in

* {@code sharedResult}. The {@code compute()} method will check

* {@code sharedResult} before proceeding with computation, so this causes

* the computation to terminate early.

*

* @param result the result found

*/

protected void shortCircuit(R result) {

if (result != null)

sharedResult.compareAndSet(null, result);

}

And the actual compute() method that does the work has this important line:

AtomicReference sr = sharedResult;

R result;

while ((result = sr.get()) == null) {

...//does the actual fork stuff here

}

where sharedResult is updated by the shortCircuit() method so the compute will see it the next time it checks the while loop condition.

EDIT

So in summary:

Threads are not interrupted

Instead, they will periodically check to see if someone has found the answer and will stop further processing if the answer has been found.

No new threads will be started once the answer has been found.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值