O'Reilly logo

Programming Concurrency on the JVM by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Groovy Integration

Groovy is a good choice among languages if we’re looking for easy metaprogramming and dynamic typing while preserving Java semantics. In this section, we’ll use the Clojure STM and the Multiverse STM through Akka in Groovy Apps. To use Multiverse directly, refer to the Multiverse documentation.

Using Clojure STM in Groovy

Using Clojure STM is as simple as using instances of clojure.lang.Ref for managed references and invoking the runInTransaction method of LockingTransaction to place a piece of code in a transaction. Let’s give it a shot using the account transfer example. Create a class called Account to hold a managed reference to its immutable state, represented by the variable currentBalance:

polyglotSTM/groovy/clojure/Transfer.groovy ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required