Guest User

clojure transaction

a guest
Feb 3rd, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns transaction-test
  2.   (:refer-clojure :exclude [time]))
  3.  
  4. (def r1 (ref 0 :min-history 20))
  5.  
  6. (def base-time (System/currentTimeMillis))
  7.  
  8. (defn time []
  9.   (- (System/currentTimeMillis) base-time))
  10.  
  11. (defn retries []
  12.   (let [count (atom 0)]
  13.     (dosync
  14.      (swap! count inc)
  15.      (Thread/sleep 1000)
  16.      (alter r1 identity))
  17.      ;;@r1)
  18.      ;;(ensure r1))
  19.     (println (format "%d retries, r1=%d" @count @r1))
  20.     (println (str "3: " (time)))))
  21.  
  22. (println (str "1: " (time)))
  23.  
  24. (-> retries Thread. .start)
  25.  
  26. (dotimes [_ 4]
  27.   (dosync
  28.    (Thread/sleep 300)
  29.    (alter r1 inc)))
  30.  
  31. (println (str "2: " (time)))
  32.  
  33. ;; Example result:
  34. ;; 1: 57
  35. ;; 2: 1309
  36. ;; 3 retries, r1=4
  37. ;; 3: 3079
Advertisement
Add Comment
Please, Sign In to add comment