Are Kotlin mutable collections thread-safe?

Viewed 2103

Despite reading the 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 in Java)

Note: I refer to the collections created by Kotlin when we do:

mutableMapOf<>()
mutableListOf<>()
mutableSetOf<>()
1 Answers

There is no thread-safety guarantee for the collections returned by mutableMapOf (MutableMap), mutableListOf (MutableList), or mutableSetOf (MutableSet).

To achieve thread safety, you can wrap them with the corresponding Java Collections wrapper:

val myThreadSafeMap = Collections.synchronizedMap(mutableMapOf())
Related