https://kotlinlang.org logo
#coroutines
Title
# coroutines
v

v0ldem0rt

07/21/2018, 9:36 PM
Using ConcurrentHashMap can block a thread coroutine is running on
v

Vsevolod Tolstopyatov [JB]

07/22/2018, 11:26 AM
it’s true, but probability of blocking is very low. If you for some reason care about blocking, you can use lock-free hash map (e.g. one from JCTools library). If you care about general utilization, it’s ok to use CHM
👍 1
1
v

v0ldem0rt

07/22/2018, 2:11 PM
JCTools looks awesome. Can help us with more data structures
g

groostav

07/22/2018, 10:50 PM
another solution is
kotlinx.collections.immutable
and
kotlinx.coroutines...Mutex
, those two in conjunction should could you pretty straight-forward concurrency.
i should say, on our team, I've had a couple accidents from our devs mixing and matching
java.util.concurrent
packages with
kotlinx.coroutines
ones, needless to say the results can be pretty strange deadlock. As a result I've put in place a "one or the other" rule, where we simply fobid mixing and matching those two packages
v

v0ldem0rt

07/23/2018, 3:35 PM
that's what my second question was, I've implemented a class called
SuspendableMap
that uses the Mutex and wraps the standard map to make it work.
v

Vsevolod Tolstopyatov [JB]

07/23/2018, 4:11 PM
Be careful with it. CHM is extremely performant (since Java 8 ), most likely
SuspendableMap
will be visibly slower. Usually it’s not worth to implement your own primitives instead of [well-tested and polished by years] already implemented ones even if there is a small probability of blocking. It’s worth doing after measuring and understanding that CHM is actually a bottleneck (which is unlikely true for an average application)
14 Views