Is Collectors.joining(quot;,quot;) thread-safe?(Collectors.joining(,) 线程安全吗?)
问题描述
java.util.stream.Collectors::joining
实现是线程安全的吗?我可以做类似的事情吗
Are java.util.stream.Collectors::joining
implementations thread-safe? Can I do something like
public final class SomeClass {
private static final Collector<CharSequence, ?, String> jc = Collectors.joining(",");
public String someMethod(List<String> someList) {
return someList.parallelStream().collect(jc);
}
}
不用担心遇到并发问题?
without fear of running into concurrency issues?
推荐答案
您可以将此收集器用作 Collectors
类中提供的任何其他收集器,而不必担心遇到并发问题.Collector
不需要关心线程安全,除非它具有 CONCURRENT
特性.它只需要使其操作不受干扰、无状态和关联.其余的将由 Stream 管道本身完成.它将以不需要额外同步的方式使用收集器功能.特别是当 accumulator
或 combiner
函数被调用时,可以保证此时没有其他线程正在对相同的累加值进行操作.这在 Collector 文档中指定:
You can use this collector as any other collector provided in Collectors
class without fear of running into concurrency issues. The Collector
need not to care about thread safety unless it has CONCURRENT
characteristic. It just need to have its operations non-interfering, stateless and associative. The rest will be done by Stream pipeline itself. It will use the collector functions in the way which does not require the additional synchronization. In particular when accumulator
or combiner
function is called, it's guaranteed that no other thread is operating on the same accumulated value at the moment. This is specified in Collector documentation:
基于 Collector
实现归约的库,例如 Stream.collect(Collector)
,必须遵守以下约束:
Libraries that implement reduction based on
Collector
, such asStream.collect(Collector)
, must adhere to the following constraints:
<...>
<...>
- 对于非并发收集器,从结果提供者、累加器或组合器函数返回的任何结果都必须是串行线程限制的.这使得收集可以并行发生,而
Collector
不需要实现任何额外的同步.归约实现必须管理输入是否正确分区,分区是单独处理的,并且只有在累积完成后才会合并.
- For non-concurrent collectors, any result returned from the result supplier, accumulator, or combiner functions must be serially thread-confined. This enables collection to occur in parallel without the
Collector
needing to implement any additional synchronization. The reduction implementation must manage that the input is properly partitioned, that partitions are processed in isolation, and combining happens only after accumulation is complete.
请注意,收集器本身和它提供的功能一样是无状态的,因此将它放在静态字段中也是安全的.状态保存在外部累加器中,由 supplier
返回并传递回 accumulator
、combiner
和 finisher
.所以即使同一个收集器被多个流操作重用,它们也不会干扰.
Note that the collector itself is stateless as well as functions it provides, thus it's also safe to have it in the static field. The state is preserved in the external accumulator which is returned by supplier
and passed back to accumulator
, combiner
and finisher
. So even if the same collector is reused by several stream operations, they don't interfere.
这篇关于Collectors.joining(",") 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Collectors.joining(",") 线程安全吗?


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01