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(",") 线程安全吗?
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01