How does one start a thread in Clojure?

Clojure fns are Runnable so it’s common to use them in exactly the way you posted, yes.

user=> (dotimes [i 10] (.start (Thread. (fn [] (println i)))))
0                                                             
1                                                             
2                                                             
4                                                             
5                                                             
3                                                             
6                                                             
7                                                             
8                                                             
9                                                             
nil

Another option is to use agents, in which case you would send or send-off and it’ll use a Thread from a pool.

user=> (def a (agent 0))
#'user/a
user=> (dotimes [_ 10] (send a inc))
nil
;; ...later...
user=> @a
10

Yet another option would be pcalls and pmap. There’s also future. They are all documented in the Clojure API.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)