Clojure best way to achieve multiple threads?

时光怂恿深爱的人放手 提交于 2019-12-08 15:29:35

问题


I am working on a MUD client written in Clojure. Right now, I need two different threads. One which receives input from the user and sends it out to the MUD (via a simple Socket), and one that reads and displays output from the MUD, to the user.

Should I just use Java Threads, or is there some Clojure-specific feature I should be turning to?


回答1:


I would recommend using the pcalls function, like this:

(defn- process-server-responses []
  (prn "server connected")
  (. java.lang.Thread sleep 1000)
  (prn "server disconnected"))

(defn- process-client-input []
  (prn "client-input start")
  (. java.lang.Thread sleep 1000)
  (prn "client-input stop"))

(pcalls process-server-responses process-client-input)

Output for the above:

"server connected"
"client-input start"
"server disconnected"
"client-input stop"

Docs for pcalls here:

http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/pcalls



来源:https://stackoverflow.com/questions/2547995/clojure-best-way-to-achieve-multiple-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!