Created
April 13, 2026 09:22
-
-
Save igrishaev/e145629f12f061f69680f2ae10d639ba to your computer and use it in GitHub Desktop.
semaphore demo
This file contains hidden or 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
| (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