开发者

Clojure best way to achieve multiple threads?

开发者 https://www.devze.com 2022-12-24 23:18 出处:网络
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

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号