Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn sleep-print-update
- [sleep-time thread-name update-fn t]
- (fn [state]
- (println (str thread-name ": " state "| " @t "ms| Time A: " @timeA "ms"))
- (Thread/sleep sleep-time)
- ;; (println (str "Sleep time: " sleep-time))
- (swap! t #(+ sleep-time %))
- (println (str thread-name ": " state "| " @t "ms"))
- (update-fn state)))
- (def counter (ref 0))
- (def timeA (atom 0))
- (def timeB (atom 0))
- (future (dosync (commute counter (sleep-print-update 100 "Thread A" inc timeA))))
- (future (dosync (commute counter (sleep-print-update 150 "Thread B" inc timeB))))
- ;Thread A: 0| 0ms| Time A: 0ms
- ;Thread B: 0| 0ms| Time A: 0ms
- ;Thread A: 0| 100ms
- ;Thread A: 0| 100ms| Time A: 100ms
- ;Thread B: 0| 150ms
- ;Thread A: 0| 200ms
- ;Thread B: 1| 150ms| Time A: 200ms
- ;Thread B: 1| 300ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement