Skip to content

Instantly share code, notes, and snippets.

@igrishaev
Created April 13, 2026 09:22
Show Gist options
  • Select an option

  • Save igrishaev/e145629f12f061f69680f2ae10d639ba to your computer and use it in GitHub Desktop.

Select an option

Save igrishaev/e145629f12f061f69680f2ae10d639ba to your computer and use it in GitHub Desktop.
semaphore demo
(defn wrap-parallel [f n]
(let [-sem (new java.util.concurrent.Semaphore (int n))]
(fn [& args]
(.acquire -sem)
(try
(apply f args)
(finally
(.release -sem))))))
(def wrapped
(wrap-parallel
(fn [x]
(let [id (.threadId (Thread/currentThread))]
(println (format "thread: %s, x: %s" id x))
(Thread/sleep 2000)))
2))
(doseq [x (range 16)]
(future
(wrapped x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment