Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concurrency 演示代码 #1

Open
jiacai2050 opened this issue Dec 16, 2017 · 1 comment
Open

concurrency 演示代码 #1

jiacai2050 opened this issue Dec 16, 2017 · 1 comment

Comments

@jiacai2050
Copy link
Owner

jiacai2050 commented Dec 16, 2017

(ns ^{:author "Jiacai Liu"
      :doc "https://segmentfault.com/l/1500000012456162"}
    demo.concurrency
  (:import java.util.concurrent.Executors))

(def nthread (+ 2 (.. Runtime getRuntime availableProcessors)))
(def executor (Executors/newFixedThreadPool nthread))

(defn do-job [workers]
  (doseq [f (.invokeAll executor workers)]
    (.get f)))

;; dynamic varbinding
(def ^:dynamic dynamic-var 123)
(do
  (println dynamic-var)
  (println (.. Thread currentThread getName))
  (future
    (binding [dynamic-var 321]
      (println (.. Thread currentThread getName))
      (println dynamic-var)))
  (println dynamic-var))

;; 很少用到,类似其他语言里面的变量
(with-local-vars [local-var 111]
  (println "test local var")
  (println (var-get local-var))
  (var-set local-var 222)
  (println (var-get local-var)))

;; Atom
(let [niters 10000
      magic-num (atom 1000)
      workers (map (fn [t]
                     (fn []
                       (dotimes [_ niters]
                         (swap! magic-num inc))))
                   (range nthread))]
  (do-job workers)
  (println "magic-num = " @magic-num))

;; Agent
(let [niters 10
      magic-num (agent 1000)
      worker (fn [n]
               (Thread/sleep 1)
               (inc n))]
  (set-agent-send-executor! executor)
  (dotimes [_ (* nthread niters)]
    (send magic-num worker)
    ;; IO operation
    (send-off magic-num worker)
    )

  (println "magic-num = " @magic-num)
  (await magic-num)
  (println "magic-num = " @magic-num))

;; Ref
(time (let [niters 10000
            run-times (atom 0)
            tom   (ref 100000)
            jerry (ref 100000)
            transfer (fn [amount]
                       (dosync
                        (swap! run-times inc)
                        (commute tom + amount)
                        ;; (alter tom + amount)
                        (alter jerry - amount)
                        ))
            workers (map (fn [t]
                           (fn []
                             (dotimes [_ niters]
                               (transfer t))))
                         (range nthread))]
        (doseq [f (.invokeAll executor workers)]
          (.get f))
        (println (format "tom = %s, jerry = %s, sum = %s, run-times = %s"
                         @tom @jerry (+ @tom @jerry) @run-times))))
@jiacai2050
Copy link
Owner Author

jiacai2050 commented Apr 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant