-
-
Save johnptoohey/5237120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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