scala 不可变集合是线程安全的吗? [英] Are scala immutable collections thread-safe?

查看:57
本文介绍了scala 不可变集合是线程安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个不可变的 scala 集合(例如 immutable.HashMap)是否是线程安全的.内容显然不会改变,但我不清楚用于优化的内部结构的可变状态.

I would like to know whether an immutable scala collection (e.g. immutable.HashMap) is thread-safe or not. The content may obviously not change, but mutable state of internals for optimization is unclear to me.

假设键和值是不可变的

感谢您的帮助

推荐答案

它们是.

每次更新都会创建新集合.为了避免不必要的副本,使用了结构共享,因此您总是可以引用某个版本的集合,并且该特定版本是线程安全的.

New collection is created with each update. To avoid unnecessary copies, structural sharing is used, therefore you always can refer to some version of collection, and that particular version is thread-safe.

这篇关于scala 不可变集合是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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