为什么我的收集器方法不能并行处理数据? [英] Why my collector method is not processing data parallely?

查看:97
本文介绍了为什么我的收集器方法不能并行处理数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


但是,假设此缩减中使用的结果容器是可同时修改的集合 - 例如ConcurrentHashMap。在这种情况下,累加器的并行调用实际上可以将它们的结果同时存入同一个共享结果容器中,从而消除了组合器合并不同结果容器的需要。这可能会提升并行执行性能。我们将此称为同时减少。

Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the parallel invocations of the accumulator could actually deposit their results concurrently into the same shared result container, eliminating the need for the combiner to merge distinct result containers. This potentially provides a boost to the parallel execution performance. We call this a concurrent reduction.


支持并发缩减的收集器标有Collector.Characteristics.CONCURRENT特性。但是,并发收集也有缺点。如果多个线程同时将结果存入共享容器,则存储结果的顺序是不确定的。

A Collector that supports concurrent reduction is marked with the Collector.Characteristics.CONCURRENT characteristic. However, a concurrent collection also has a downside. If multiple threads are depositing results concurrently into a shared container, the order in which results are deposited is non-deterministic.

来自< a href =https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html\"rel =nofollow noreferrer>文件

这意味着与供应商(Concurrent-thread-safe)的collect方法应该具有 Collector.Characteristics.CONCURRENT 。因此不应该维持任何订单。

this means the collect method with supplier(Concurrent-thread-safe)should have Collector.Characteristics.CONCURRENT. and thus should not maintain any order.

但是我的代码




List<Employee> li=Arrays.asList(Employee.emparr());
        System.out.println("printing concurrent result "+li.stream().parallel().unordered().map(s->s.getName()).collect(() -> new ConcurrentLinkedQueue<>(),
                (c, e) -> c.add(e.toString()),
                (c1, c2) -> c1.addAll(c2))
                                                  .toString());

始终以遇到的顺序打印结果。这是否意味着我的 Collector.Characteristics不是CONCURRENT ?如何检查和设置这个特性?

always prints the result in the encountered order. does this mean my Collector.Characteristics is not CONCURRENT ? how to check and set this characteristics ?

推荐答案

您的收集器不知道您使用提供的并发收集通过供应商,只需添加特征并查看它是否按您希望的方式执行;例如:

Your Collector does not know that you use a concurrent collection provided by Supplier, just add the characteristic and see that it is executed the way you want to; for example:

String s = Stream.of(1, 2, 3, 4).parallel()
            .unordered()
            .collect(
                    Collector.of(
                            () -> new ConcurrentLinkedQueue<>(),
                            (c, e) -> c.add(e.toString()),
                            (c1, c2) -> {
                                c1.addAll(c2);
                                return c1;
                            },
                            Characteristics.CONCURRENT))

这篇关于为什么我的收集器方法不能并行处理数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆