Kotlin 可变集合是线程安全的吗?
Are Kotlin mutable collections thread-safe?
尽管阅读了 Java 中的 Kotlin's documentation about collections I can't find if Kotlin's mutable collections are thread-safe and if there are any concurrent alternative implementations (Like HashMap vs ConcurrentHashMap)
注意:我参考的是 Kotlin 创建的集合:
mutableMapOf<>()
mutableListOf<>()
mutableSetOf<>()
mutableMapOf
(MutableMap
)、mutableListOf
(MutableList
) 或 mutableSetOf
( MutableSet
).
为了实现线程安全,你可以用相应的Java集合包装器来包装它们:
val myThreadSafeMap = Collections.synchronizedMap(mutableMapOf())
尽管阅读了 Java 中的 Kotlin's documentation about collections I can't find if Kotlin's mutable collections are thread-safe and if there are any concurrent alternative implementations (Like HashMap vs ConcurrentHashMap)
注意:我参考的是 Kotlin 创建的集合:
mutableMapOf<>()
mutableListOf<>()
mutableSetOf<>()
mutableMapOf
(MutableMap
)、mutableListOf
(MutableList
) 或 mutableSetOf
( MutableSet
).
为了实现线程安全,你可以用相应的Java集合包装器来包装它们:
val myThreadSafeMap = Collections.synchronizedMap(mutableMapOf())