Is it ok to use ConcurrentHashMap with coroutines?...
# coroutines
a
Is it ok to use ConcurrentHashMap with coroutines? What dispatcher should I generally use with it?
my use case: I have potentially long reads from map - iterate over all entries doing something with each. During the reads there could be potential write requests, which I don't want to block ConcurrentHashMap guarantees on concurrent reads are ok in my case. The only question is, as ConcurrentHashMap is lock based and blocking, should I use Dispatchers.IO with it?
p
IMO default dispatcher is fine — only use IO for actual IO Or use a normal map with suspending mutex lock Not sure which would perform better
On another topic, are you the reporter of https://github.com/Kotlin/kotlinx.coroutines/issues/3757? Did you find an explanation for it?
a
Yes, I am the reporter
When calling cancel() on parent 1) ProducerCoroutine::onCancelled is called 2) ChannelCoroutine::cancel is called
ChannelCoroutine::cancel is supposed to call
channel.cancel()
, but it does not happen because there
isCanceled
check
And ProducerCoroutine::onCancelled calls
channel.close(cause)
instead of
channel.cancel()
In short I don't know what is happening, and I am not convinced it is the expected behaviour 🙂