interop

pinealan 2025-06-02T20:41:54.333579Z

Does anyone use ConcurrentHashMaps in their clojure code? Is it idiomatic to use directly use anything in java.util.concurrent at all?

Alex Miller (Clojure team) 2025-06-02T20:42:43.055499Z

sure

Alex Miller (Clojure team) 2025-06-02T20:43:01.318419Z

CHM, queues, occasionally I use latches

pinealan 2025-06-02T20:46:54.047179Z

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?

Alex Miller (Clojure team) 2025-06-02T20:50:11.922689Z

an atom + a Clojure PHM is much less concurrent (single-threaded access) than using a CHM

Alex Miller (Clojure team) 2025-06-02T20:51:54.394519Z

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

👍 1
Alex Miller (Clojure team) 2025-06-02T20:52:49.524509Z

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

Alex Miller (Clojure team) 2025-06-02T20:53:38.225369Z

often I end up reaching for that kind of stuff when writing tests for concurrent things

pinealan 2025-06-02T21:04:43.333219Z

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

pinealan 2025-06-02T21:05:30.949899Z

And now I know what to reach for next time when I run into bottlenecks with my atoms, thanks!

Alex Miller (Clojure team) 2025-06-02T22:07:48.819999Z

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

exitsandman 2025-06-03T04:52:18.927409Z

And of course any operation on a ConcurrentMap that requires multiple lookups needs synchronization.