Skip to content

Instantly share code, notes, and snippets.

@pingles
Created June 9, 2014 15:56
Show Gist options
  • Save pingles/d12314ecdff0a319091b to your computer and use it in GitHub Desktop.
Save pingles/d12314ecdff0a319091b to your computer and use it in GitHub Desktop.
(ns async.example
(:require [clojure.core.async :refer (chan go-loop <! >!)])
(:import [java.util Date])
(:gen-class))
(defn producer-process [channel]
(go-loop []
(>! channel (str "Hello, the time is " (Date.)))
(Thread/sleep 500)
(recur)))
(defn consumer-process [channel]
(go-loop [message (<! channel)]
(println "Received:" message)
(recur (<! channel))))
(defn wait! []
(let [s (java.util.concurrent.Semaphore. 0)]
(.acquire s)))
(defn -main [& args]
(let [c (chan)]
(producer-process c)
(consumer-process c)
(wait!)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment