使用 channel 时,为 future
推荐或者是thread
?是否有时间 future
更有意义?
Rich Hickey 在 core.async 上的博文建议使用 thread
而不是 future
:
While you can use these operations on threads created with e.g. future, there is also a macro, thread , analogous to go, that will launch a first-class thread and similarly return a channel, and should be preferred over future for channel work.
~ http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html
但是,core.async 示例广泛使用了
future
。使用 channel 时:(defn fake-search [kind]
(fn [c query]
(future
(<!! (timeout (rand-int 100)))
(>!! c [kind query]))))
~ https://github.com/clojure/core.async/blob/master/examples/ex-async.clj
最佳答案
概括
一般情况下,thread
对于应用程序中 channel 突出的部分,它的 channel 返回可能会更方便。另一方面,应用程序中的任何子系统在其边界与某些 channel 接口(interface)但内部不使用 core.async 时,应该可以随意以对它们最有意义的方式启动线程。thread
之间的差异和 future
正如您引用的 core.async 博客文章的片段中所指出的那样,thread
返回一个 channel ,就像 go
:
(let [c (thread :foo)]
(<!! c))
;= :foo
channel 由大小为 1 的缓冲区支持,并在
thread
的主体返回值后关闭。表格放在上面。 (除非返回的值恰好是 nil
,在这种情况下, channel 将被关闭而不放置任何东西——core.async channel 不接受 nil
。)这使得
thread
与 core.async 的其余部分完美契合。特别是,这意味着 go
+ 单次爆炸操作和 thread
+ double-bang ops在代码结构上的使用方式确实相同,您可以使用alt!
中的返回 channel /alts!
(以及双爆炸等价物)等等。相比之下,
future
的返回可以是deref
'd ( @
) 以获取 future
的返回值表单的主体(可能是 nil
)。这使得 future
非常适合不使用 channel 的常规 Clojure 代码。使用的线程池还有另一个区别——
thread
使用 core.async 特定的线程池,而 future
使用代理支持池之一。当然所有的双爆操作,以及
put!
和 take!
,无论调用它们的线程的启动方式如何,都可以正常工作。
关于multithreading - future 与线程 : Which is better for working with channels in core. 异步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20625390/