Java 8stream parallel,Java 8 parallelStream findFirst

Suppose we have a list of workers like this :

List workers = new ArrayList<>();

workers.add(new Worker(1));

workers.add(new Worker(2));

workers.add(new Worker(3));

workers.add(new Worker(4));

workers.add(new Worker(5));

I want to find the first worker who finished his job, so :

Worker first = workers.parallelStream().filter(Worker::finish).findFirst().orElse(null);

but there's a problem, I don't want to wait for all workers to finish their jobs and then find the first, but the first worker As soon as he finished his job!

public class Test {

public static void main(String[] args) {

List workers = new ArrayList<>();

workers.add(new Worker(1));

workers.add(new Worker(2));

workers.add(new Worker(3));

workers.add(new Worker(4));

workers.add(new Worker(5));

Worker first = workers.parallelStream().filter(Worker::finish).findFirst().orElse(null);

if (first != null) {

System.out.println("id : " + first.id);

}

}

static class Worker {

int id;

Worker(int id) {

this.id = id;

}

boolean finish() {

int t = id * 1000;

System.out.println(id + " -> " + t);

try {

Thread.sleep(t);

} catch (InterruptedException ignored) {

}

return true;

}

}

}

is there any way to achieve it using java.util.Stream?

Thanks.

解决方案

When you use your finish method as the filter of the Stream, it means that in order to evaluate the filter's predicate for a specific Worker, the Worker has to finish its work.

When you run this code as a parallel Stream, however, it's possible that the filter would be applied on multiple Workers at the same time, in which case, the first one to finish would give you the output. However, you have no control over how many threads the parallel Stream will use. It may decide that some of the Workers should be processed on the same thread, in which case some of them won't be processed at all (since your terminal operation requires that only one Worker finishes its processing).

Therefore, if your goal is that finish is executed for all Workers at the same time, you can't use a Stream (not even a parallel Stream).

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值