/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
require "java" require "clojure.jar" include_class "clojure.lang.LockingTransaction" include_class "clojure.lang.Ref" my_account = Ref.new(1) puts "Initial value #{my_account.deref}" begin my_account.set(1000) rescue java.lang.IllegalStateException puts "Reference updated outside a transaction.. Naughty.. :)" end puts "Value after failed update #{my_account.deref}" Thread.new {LockingTransaction.run_in_transaction(Proc.new { my_account.set 1000 })} Thread.new {LockingTransaction.run_in_transaction(Proc.new { sleep 0.1; my_account.set 21000 })} # Will fail because get commited after first transaction puts "Value after successful transaction #{my_account.deref}"