我是clojure的新手,我正在尝试理解如何正确使用它的并发功能,所以任何批评/建议都值得赞赏.所以我试图在clojure中编写一个小测试程序,其工作方式如下:
以下是我对上述每个步骤的计划:
Mic*_*zyk 24
这是我的看法.我特别指出只使用Clojure数据结构来看看它是如何工作的.请注意,从Java工具箱中获取阻塞队列并在此处使用它是完全通常和惯用的.我想,代码很容易适应.更新:我实际上已经适应了它java.util.concurrent.LinkedBlockingQueue,见下文.
打电话(pro-con)开始试运行; 然后查看内容,output看是否发生了任何事情,queue-lengths看看它们是否保持在给定范围内.
更新:为了解释为什么我觉得需要在ensure下面使用(我在IRC上被问到这个问题),这是为了防止写入偏斜(请参阅维基百科关于快照隔离的文章定义).如果我替换@queue了(ensure queue),那么两个或更多生产者可以检查队列的长度,发现它小于4,然后在队列上放置其他项目,并可能使队列的总长度超过4,打破约束.同样,两个消费者@queue可以接受相同的项目进行处理,然后从队列中弹出两个项目.ensure防止这些情况发生.
(def go-on? (atom true))
(def queue (ref clojure.lang.PersistentQueue/EMPTY))
(def output (ref ()))
(def queue-lengths (ref ()))
(def *max-queue-length* 4)
(defn overseer
([] (overseer 20000))
([timeout]
(Thread/sleep timeout)
(swap! go-on? not)))
(defn queue-length-watch [_ _ _ new-queue-state]
(dosync (alter queue-lengths conj (count new-queue-state))))
(add-watch queue :queue-length-watch queue-length-watch)
(defn producer [tag]
(future
(while @go-on?
(if (dosync (let [l (count (ensure queue))]
(when (< l *max-queue-length*)
(alter queue conj tag)
true)))
(Thread/sleep (rand-int 2000))))))
(defn consumer []
(future
(while @go-on?
(Thread/sleep 100) ; don't look at the queue too often
(when-let [item (dosync (let [item (first (ensure queue))]
(alter queue pop)
item))]
(Thread/sleep (rand-int 500)) ; do stuff
(dosync (alter output conj item)))))) ; and let us know
(defn pro-con []
(reset! go-on? true)
(dorun (map #(%1 %2)
(repeat 5 producer)
(iterate inc 0)))
(dorun (repeatedly 2 consumer))
(overseer))
Run Code Online (Sandbox Code Playgroud)
上面写的一个版本使用LinkedBlockingQueue.请注意代码的大致轮廓基本相同,其中一些细节实际上稍微清晰一些.我queue-lengths从这个版本中移除了,因为LBQ我们处理了这个约束.
(def go-on? (atom true))
(def *max-queue-length* 4)
(def queue (java.util.concurrent.LinkedBlockingQueue. *max-queue-length*))
(def output (ref ()))
(defn overseer
([] (overseer 20000))
([timeout]
(Thread/sleep timeout)
(swap! go-on? not)))
(defn producer [tag]
(future
(while @go-on?
(.put queue tag)
(Thread/sleep (rand-int 2000)))))
(defn consumer []
(future
(while @go-on?
;; I'm using .poll on the next line so as not to block
;; indefinitely if we're done; note that this has the
;; side effect that nulls = nils on the queue will not
;; be handled; there's a number of other ways to go about
;; this if this is a problem, see docs on LinkedBlockingQueue
(when-let [item (.poll queue)]
(Thread/sleep (rand-int 500)) ; do stuff
(dosync (alter output conj item)))))) ; and let us know
(defn pro-con []
(reset! go-on? true)
(dorun (map #(%1 %2)
(repeat 5 producer)
(iterate inc 0)))
(dorun (repeatedly 2 consumer))
(overseer))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3149 次 |
| 最近记录: |