How does one start a thread in Clojure?

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

    Yes, the way that you start a Java Thread in Clojure is something like what you have there.

    However, the real question is: why would you want to do that? Clojure has much better concurrency constructs than threads.

    If you look at the canonical concurrency example in Clojure, Rich Hickey's ant colony simulation, you will see that is uses exactly 0 threads. The only reference to java.lang.Thread in the entire source is three calls to Thread.sleep, whose sole purpose is to slow the simulation down so that you can actually see what is going on in the UI.

    All the logic is done in Agents: one agent for every ant, one agent for the animation and one agent for the pheromone evaporation. The playing field is a transactional ref. Not a thread nor lock in sight.

提交回复
热议问题