How does one start a thread in Clojure?

后端 未结 7 723
你的背包
你的背包 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:01

    Usually when I want to start a thread in Clojure I just use future.

    As well as being simple to use, this has the advantage that you avoid having to do any messy Java interop to access the underlying Java threading mechanisms.

    Example usage:

    (future (some-long-running-function))
    

    This will execute the function asynchronously in another thread.

    (def a (future (* 10 10)))
    

    If you want to get the result, just dereference the future, e.g:

    @a
    => 100
    

    Note that @a will block until the future thread has completed its work.

提交回复
热议问题