4
如果我使用以下代码启动线程,如何捕获/检测线程本身内的退出调用,以便我可以进行一些清理它终止之前?如何检测线程中的Ruby线程何时死亡
thr = Thread.new { sleep }
thr.status # => "sleep"
thr.exit
thr.status # => false
我在想,也许它会像下面这样,但我不确定。
thr = Thread.new {
begin
sleep
rescue StandardError => ex
puts ex.message
rescue SystemExit, Interrupt
puts 'quit'
ensure
puts 'quit'
end
}
thr.status # => "sleep"
thr.exit #=> "quit"
thr.status # => false
你不能。线程#停止是一个无法消除的大锤。如果您想通过清理来发信号终止或取消,则需要使用更微妙的方法。 – Linuxios 2013-03-15 21:23:10