這似乎是一個未公開的要求,即restart-agent
在不同的線程比錯誤處理程序被調用:
(dotest
(future (println "running in a thread..."))
(let [agt (agent 0)
; This doesn't work
h01 (fn [a e]
(println :10 "agent error found:")
(println :11 "restarting agent...")
(restart-agent a 100)
(Thread/sleep 100)
(println :12 "agent restarted, state=" @a))
; This works. Need to call restart-agent in a separate thread
h02 (fn [a e]
(println :20 "agent error found:")
(future
(println :21 "restarting agent...")
(restart-agent a 200)
(println :22 "agent restarted, state=" @a))) ;=> 200
]
(set-error-handler! agt h02)
(send agt inc)
(Thread/sleep 100) (spy :01 @agt) ;=> 1
(Thread/sleep 100) (send agt #(/ % 0))
(Thread/sleep 100) (spy :02 @agt) ;=> 200
(Thread/sleep 100) (send agt inc)
(Thread/sleep 100) (spy :03 @agt) ;=> 201
))
有了結果:
running in a thread...
:01 => 1
:20 agent error found:
:21 restarting agent...
:22 agent restarted, state= 200
:02 => 200
:03 => 201
我標誌着這是正確的答案,因爲它幫助我瞭解問題的真正原因,這意味着由此我明白了爲什麼@ AlanThompson的答案(即使用未來的)工作。 – Integralist