SHARE
TWEET

Untitled

a guest Nov 21st, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn sleep-print-update
  2.              [sleep-time thread-name update-fn t]
  3.              (fn [state]
  4.                (println (str thread-name ": " state "| " @t "ms| Time A: " @timeA "ms"))
  5.                (Thread/sleep sleep-time)
  6.                ;; (println (str "Sleep time: " sleep-time))
  7.                (swap! t #(+ sleep-time %))
  8.                (println (str thread-name ": " state "| " @t "ms"))
  9.                (update-fn state)))
  10.  
  11. (def counter (ref 0))
  12. (def timeA (atom 0))
  13. (def timeB (atom 0))
  14. (future (dosync (commute counter (sleep-print-update 100 "Thread A" inc timeA))))
  15. (future (dosync (commute counter (sleep-print-update 150 "Thread B" inc timeB))))
  16. ;Thread A: 0| 0ms| Time A: 0ms
  17. ;Thread B: 0| 0ms| Time A: 0ms
  18. ;Thread A: 0| 100ms
  19. ;Thread A: 0| 100ms| Time A: 100ms
  20. ;Thread B: 0| 150ms
  21. ;Thread A: 0| 200ms
  22. ;Thread B: 1| 150ms| Time A: 200ms
  23. ;Thread B: 1| 300ms
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top