Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BUNotesAI/6c80eed40a4b3b9665a4a04dadfe5f93 to your computer and use it in GitHub Desktop.

Select an option

Save BUNotesAI/6c80eed40a4b3b9665a4a04dadfe5f93 to your computer and use it in GitHub Desktop.
promise 确定性死锁
;;
;;Executor线程池多次执行方法
;;
(import 'java.util.concurrent.Executors)
(def thread-pool (Executors/newFixedThreadPool (+ 2 (.availableProcessors (Runtime/getRuntime)))))
(defn dothreads! [f & {thread-count :threads
exec-count :times
:or {thread-count 1 exec-count 1}}]
(dotimes [t thread-count]
(.submit thread-pool #(dotimes [c exec-count] (f c)))))
;; 确定性死锁
(def alex (promise))
(def kate (promise))
(dothreads! (println "alex has " @alex) (deliver kate :thinking))
(dothreads! (println "kate has " @kate ) (deliver alex :fork))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment