Clojure最好的方式来实现多线程? [英] Clojure best way to achieve multiple threads?

查看:141
本文介绍了Clojure最好的方式来实现多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个用Clojure编写的MUD客户端。现在,我需要两个不同的线程。一个接收来自用户的输入并通过一个简单的Socket发送到MUD,另一个从MUD读取并显示输出到用户。

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.

我应该使用Java Threads,还是有一些Clojure特有的功能,我应该转向?

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

推荐答案

使用pcalls函数,如下所示:

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)

上述输出:

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

这里的pcalls文件:

Docs for pcalls here:

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

这篇关于Clojure最好的方式来实现多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆