Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created October 12, 2011 14:05
Show Gist options
  • Save passingloop/1281302 to your computer and use it in GitHub Desktop.
Save passingloop/1281302 to your computer and use it in GitHub Desktop.
7l7w-clojure-day-3
(ns passingloop.sleepy_barber)
(def rnd (new java.util.Random))
(defn rush [_ chairs]
(do
(Thread/sleep (+ 10 (. rnd nextInt 20)))
(. chairs offer :customer)
(recur :dummy chairs)))
(defn haircut [_ chairs counter]
(do
(. chairs take)
(Thread/sleep 20)
(swap! counter inc)
(recur :dummy chairs counter)))
(defn sleepy-barber []
(let [chairs (new java.util.concurrent.ArrayBlockingQueue 3)
counter (atom 0)
barber (agent 0)
customers (agent 0)]
(send barber haircut chairs counter)
(send customers rush chairs)
(await-for 10000 barber customers)
(println (str "cut: " @counter ", barber: " @barber ", customers: " @customers))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment