11

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?

Timothy McDowell
  • 237
  • 2
  • 10
  • http://clojure.org/concurrent_programming is helpful – Alex Stoddard Mar 31 '10 at 15:59
  • 4
    "In all cases, Clojure does not replace the Java thread system, rather it works with it. Clojure functions are java.util.concurrent.Callable, therefore they work with the Executor framework etc." – Alex Stoddard Mar 31 '10 at 16:00

1 Answers1

8

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

Tim Harper
  • 2,561
  • 20
  • 23