-
Notifications
You must be signed in to change notification settings - Fork 0
/
barber.clj
61 lines (50 loc) · 1.5 KB
/
barber.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(ns clojure_seven.barber)
(def max-chairs 3)
(def time-open (* 10 1000))
(def length-of-haircut 20)
(def shop-open? (atom false))
(def barber-busy? (ref false))
(def open-chairs (ref max-chairs))
(def lost-customers (ref 0))
(def haircut-count (ref 0))
(def customer-waiting? #(< @open-chairs max-chairs))
(def work-to-do? #(or @shop-open? (customer-waiting?)))
(def chair-available? #(and (customer-waiting?) (not @barber-busy?)))
(defn generate-customers []
(future
(while @shop-open?
(let [tt-next-cust (+ (rand-int 20) 10)]
(Thread/sleep tt-next-cust)
(dosync
(if (> @open-chairs 0)
(alter open-chairs dec)
(alter lost-customers inc)))))))
(defn give-haircut []
(dosync
(ref-set barber-busy? true)
(alter open-chairs inc))
(Thread/sleep length-of-haircut)
(dosync
(ref-set barber-busy? false)
(alter haircut-count inc)))
(defn operate-shop []
(future (while (work-to-do?) (if (chair-available?) (give-haircut)))))
(defn print-progress []
(print "\rI've given" @haircut-count "haircuts, lost"
@lost-customers "customers, and have" @open-chairs
"seats available.")
(flush))
(defn monitor-progress []
(future (while (work-to-do?) (print-progress) (Thread/sleep 100))))
(defn open-shop []
(reset! shop-open? true)
(generate-customers)
(operate-shop)
(monitor-progress)
(Thread/sleep time-open)
(reset! shop-open? false))
(open-shop)
(Thread/sleep 60)
(print-progress)
(println " Done!")
(shutdown-agents)