Skip to content

Instantly share code, notes, and snippets.

@johnptoohey
Forked from gorsuch/gist:4617330
Last active December 15, 2015 09:19
Show Gist options
  • Save johnptoohey/5237120 to your computer and use it in GitHub Desktop.
Save johnptoohey/5237120 to your computer and use it in GitHub Desktop.
Taken from [here](http://blog.marrowboy.co.uk/2011/10/21/worker-queues-in-clojure/), recording so I don't lose it:
```clj
(defn new-q [] (java.util.concurrent.LinkedBlockingDeque.))
(defn offer!
"adds x to the back of queue q"
[q x] (.offer q x) q)
(defn take!
"takes from the front of queue q. blocks if q is empty"
[q] (.take q))
```
```clj
(def example-queue (new-q))
;; background thread, blocks waiting for something to print
(future (println ">> " (take! example-queue)))
;; the future-d thread will be unblocked and ">> HELLO WORLD" is printed.
(offer! example-queue "HELLO WORLD")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment