我是clojure的新手并且正在尝试理解如何正确使用其并发功能,因此任何批评/建议都会受到赞赏。 所以我试图在clojure中编写一个小的测试程序,其工作原理如下:
以上是我对上述每一步的计划:
答案 0 :(得分:24)
这是我的看法。我特别指出只使用Clojure数据结构来看看它是如何工作的。请注意,从Java工具箱中获取阻塞队列并在此处使用它是完全通常和惯用的。我想,代码很容易适应。 更新:我确实将其改编为java.util.concurrent.LinkedBlockingQueue
,见下文。
致电(pro-con)
开始试运行;然后查看output
的内容以查看是否发生了任何事情,并queue-lengths
查看它们是否保持在给定范围内。
更新:为了解释为什么我觉得需要使用下面的ensure
(我在IRC上被问过这个问题),这是为了防止写入歪斜(请参阅维基百科关于{的文章{3}}定义)。如果我用@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))
使用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))