在一些进程中,我得到了一些由core.async/thread返回的线程,我正要关闭它们。我没有关闭我的整个程序,只关闭了这些线程。如何终止线程?
Java Thread类的.stop方法已被弃用,但我很乐意使用它,只不过core.async/thread返回的不是Thread,而是ManyToManyChannel
user=> (clojure.core.async/thread)
#object[clojure.core.async.impl.channels.ManyToManyChannel 0x780e97c0
"clojure.core.async.impl.channels.ManyToManyChannel@780e97c0"]
user=> (type *1)
clojure.core.async.impl.channels.ManyToManyChannel我还没有找到任何关于ManyToManyChannel的文档。这听起来像是线程类型的奇怪名称,所以这里可能有一些我不理解的基本内容。但这是我现在天真的,听起来很荒唐的问题:如何杀死一个ManyToManyChannel
clojure.repl/thread-stopper似乎对ManyToManyChannel%s没有影响。
发布于 2016-06-03 01:29:46
你让线程自然地终止。如果外部终止是必要的,你必须实现它。
(defn terminatable [input-ch terminate-ch]
(thread
(loop []
(let [[v ch] (alts!! [input-ch terminate-ch])]
(if (identical? ch input-ch)
(if (some? v)
(do (process-input v) (recur))
;; else input-ch has closed -> don't call recur,
;; thread terminates
)
;; else we received sth. from terminate-ch,
;; or terminate-ch has closed -> don't call recur,
;; thread terminates
)))))然后通过(close! terminate-ch)在外部终止
最后,您可以通过从thread返回的通道获取数据来确定线程何时终止。
也就是说。
(take! (terminatable (chan) (doto (chan) close!))
(fn [_] (println "Thread is terminated")))https://stackoverflow.com/questions/37598084
复制相似问题