view 6-Clojure/blockqingqueue.clj @ 84:920b50be0fe5

Add notes (and failed code) for blocking queue
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Jun 2019 12:07:10 +0100
parents
children
line wrap: on
line source

(defn create_blocking_queue [] (atom []))

(defn add_item [queue item]
    (swap! queue conj item))

(defn pop_item [queue]
    (future
        (
            (while (empty? @queue) (do
                (Thread/sleep 1000)
                (print ".")
            ))
            ;(@queue 0)
        )
    )
)

(def myqueue (create_blocking_queue))
; TODO: Add something after a period of time
(def updater (agent myqueue))
(send updater (fn [_q item] (Thread/sleep 5000) (add_item _q item) (println "Item added")) :item)
(println "Scheduled an item")
(def first_item (pop_item myqueue))
(println "Popped an item (future)")
(println first_item)
(println @first_item)