Fork me on GitHub
#braveandtrue
<
2017-02-13
>
jeffh-fp20:02:21

(defn sleep-print-update
  [sleep-time thread-name update-fn]
  (fn [state]
    (Thread/sleep sleep-time)
    (println (str thread-name ": " state))
    (update-fn state)))
(def counter (ref 0))
(future (dosync (commute counter (sleep-print-update 100 "Thread A" inc))))
(future (dosync (commute counter (sleep-print-update 150 "Thread B" inc))))
Here’s a timeline of what prints:
Thread A: 0 | 100ms
Thread B: 0 | 150ms
Thread A: 0 | 200ms
Thread B: 1 | 300ms

jeffh-fp20:02:28

from Chapter 10

jeffh-fp20:02:49

I'm confused why println is called twice

jeffh-fp20:02:53

(per thread)

jeffh-fp20:02:15

commute shouldn't be retrying the "transaction"