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

JRuby Integration

JRuby brings the power of Ruby along with its elegance, expressiveness, and conciseness to the Java platform. In this section, we’ll use Clojure STM and Multiverse STM through Akka in JRuby code. If you’d like to use Multiverse directly, refer to the Multiverse documentation for the JRuby integration API.

Using Clojure STM in JRuby

To use Clojure STM from JRuby, we’ll use clojure.lang.Ref for managed references and the runInTransaction method of LockingTransaction for the transaction. Let’s give that a try using the account transfer example. Create a class called Account that will hold a managed reference to its immutable state represented by the JRuby field @balance:

polyglotSTM/jruby/clojure/account.rb
 
require ​'java'

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