Does anyone use ConcurrentHashMaps in their clojure code? Is it idiomatic to use directly use anything in java.util.concurrent at all?
sure
CHM, queues, occasionally I use latches
Thanks for the quick reply! (it was faster than I could type up more background on my question) For a bit more context, I’ve been a Clojure user for a few years now and have never much used Java (except the occasional only course work); Been recently trying to understand the JVM at a deeper level and approached it by learning Java. So coming across the concurrent utils module which so many praise to be one of the best in any languages naturally led me to ask how one would use these in Clojure. In my concurrent/parallel workloads, I’ve only every used clj synchronisation primitives (atoms/agents), and I wonder what are the situations that one would instead use these Java data structures?
an atom + a Clojure PHM is much less concurrent (single-threaded access) than using a CHM
staying in Clojure is fine for lots of use cases, but I've used a CHM when I needed that increased concurrency. if you need a queue with access from multiple threads, Clojure's PQ + atom is usually a poor fit - better to use either core.async channels or any of the queues and deques in Java, which are great
the synchronization stuff like latches, phasers, cyclicbarriers are less common (usually I end up decoupling things with queues) but those are all great when you need that
often I end up reaching for that kind of stuff when writing tests for concurrent things
I see. On queues, in my stack I typically use manifold instead of core.async, which iirc uses java queues under the hood, so I guess I’ve already been benefiting from these perf improvements
And now I know what to reach for next time when I run into bottlenecks with my atoms, thanks!
do keep in mind that mutable/concurrent data structures of course don't give you the benefits of persistence as they bash in place - this is the tradeoff
And of course any operation on a ConcurrentMap that requires multiple lookups needs synchronization.