Is it correct to use ConcurrentHashMaps in the con...
# coroutines
s
Is it correct to use ConcurrentHashMaps in the context of multiple coroutines? As in multiple coroutines can essentially run in a single thread and these data structures are thread safe, but in case multiple coroutines are running on that same thread. Would it be safe then?
e
coroutines can only switch at suspend points which only occur in Kotlin code; Java code always runs sequentially
👍 1
so if you're doing something like
map[key]
in a single-threaded dispatcher, sure that's safe
but if you're doing
map[key] = map[key] + foo()
where
foo()
is a suspending function, that's potentially problematic (just as in threaded code)
o
So it's generally single-threaded: yes, safe, but unnecessary, multi-threaded: yes, safe.
👍 1