How does one start a thread in Clojure?

后端 未结 7 718
你的背包
你的背包 2021-01-30 10:49

I\'ve read a lot about how great Clojure is when it comes to concurrency, but none of the tutorials I\'ve read actually explain how to create a thread. Do you just do (.start (T

相关标签:
7条回答
  • 2021-01-30 11:28

    The (future f) macro wraps the form f in a Callable (via fn*) and submits that to a thread pool immediately.

    if you need a reference to a java.lang.Thread object, for instance, to use it as a java.lang.Runtime shutdown hook, you can create a Thread like this:

    (proxy [Thread] [] (run [] (println "running")))
    

    This will not start the thread yet, only create it. To create and run the thread, submit it to a thread pool or call .start on it:

    (->
     (proxy [Thread] [] (run [] (println "running")))
     (.start))
    

    Brians's answer also creates a thread but doesn't need proxy, so that's very elegant. On the other hand, by using proxy we can avoid creating a Callable.

    0 讨论(0)
提交回复
热议问题