Java Integration

Although Java is showing its age, it’s still one of the most widely used language. In this section, we’ll see that selecting a concurrency model does not force us down the path of language selection. We’ll reach out to the STM models right from within Java. We’ll have to endure some Java-induced verbosity and have to ensure immutability; however, it’s a breeze to use the STM API itself, as we’ll see.

Using Clojure STM in Java

We can use Clojure STM for Java quite easily because Ref and LockingTransaction are exposed as simple classes. The runInTransaction method takes as a parameter an instance that implements the Callable interface. So, wrapping code in a transaction is as simple as wrapping it in the Callable interface’s ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.