is there a thread-safe version of `mutableMapOf()`...
# announcements
l
is there a thread-safe version of
mutableMapOf()
? i need the map to be thread-safe, and also hold its entries in the order of insertion.
k
Collections.synchronizedMap(LinkedHashMap())
👍 3
Will get you some barebones thread safety.
l
thank you!
k
But keep in mind that you need to be very careful, eg doing things like
Copy code
if (x in map) println(map[x])
is still thead-unsafe.
l
I see. thanks
b
is it ever better to use
Collections.synchronizedMap(LinkedHashMap())
over
ConcurrentHashMap
?
k
If you want insertion order to be kept, otherwise no,
ConcurrentHashMap
is way better.
synchronizedMap
just naively puts a single lock on everything,
ConcurrentHashMap
actually allows things like overlapping reads etc.
b
ah right, i guess synchronizedMap can be used on any map type (like Linked) which could be nice if you don't mind the naive synchronization