如果我将来包装一个函数并在命令行上从leiningen调用它,它会向运行时添加1分钟。谁能告诉我为什么会这样?你能告诉我如何阻止这种行为吗?我想避免这一分钟。
示例代码:
(ns futest.core
(:gen-class))
(defn testme []
(Thread/sleep 2000)
42)
(defn -main
[& args]
(if (= (nth args 0) "a")
;; either run a simple function that takes 2 seconds
(do
(println "no future sleep")
(let [t (testme)]
(println t))
(println "done."))
;; or run a simple function that takes 2 seconds as a future
(do
(println "future sleep")
(let [t (future (testme))]
(println @t))
(println "done.")
;; soo, why does it wait for 1 minute here?
)))
答案 0 :(得分:3)
这是因为代理使用two threadpools,第一个是固定的线程池,第二个是缓存的线程池。缓存的线程池终止在特定持续时间the default being 60 seconds内处于非活动状态的运行线程。这就是你看到60秒延迟的原因。当然,如果您手动调用shutdown-agents,这两个线程池都会终止,不会阻止您退出的非守护程序线程。
答案 1 :(得分:2)
正如this question的回答中所述,您需要在shutdown-agents
方法结束时致电-main
。
由于该问题未提及未来,因此我将其发布为自我回答的Q& A,因此它没有出现在我的谷歌搜索中。果然,如果我补充:
;; soo, why does it wait for 1 minute here?
(shutdown-agents)
)))
问题消失了。