Why does a clojure future block the main thread? -


i have trivial lein project -main contains future:

(def f (future 42))  (defn -main [& args]   (println @f)) 

when run lein run prints 42 not return.

i don't understand why not return ?

how lein run return ?

your question twofold:

  1. why lein not return?

lein hangs because thread pool backs clojure futures not use daemon threads have explicitly shut down. if change code following, should work:

(def f (future 42))  (defn -main [& args]   (println @f)   (shutdown-agents)) 
  1. futures block main thread

the line (println @f) can potentially block main thread when "derefing" f if future hasn't finished job yet.

this limitation of clojure futures can addressed using core.async or rxclojure. i've been working on alternative implementation of futures clojure plan open source , addresses these issues.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -