Why my collector method is not processing data parallely?

后端 未结 1 1557
有刺的猬
有刺的猬 2021-01-14 06:56

Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the

相关标签:
1条回答
  • 2021-01-14 07:57

    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))
    
    0 讨论(0)
提交回复
热议问题