Ruby 当thread.abort_on_exception为true时,是否有任何方法可以避免从线程引发错误?
Ruby 2.0 mingw32Ruby 当thread.abort_on_exception为true时,是否有任何方法可以避免从线程引发错误?,ruby,multithreading,Ruby,Multithreading,Ruby 2.0 mingw32 视窗7 我想执行这段代码,但我无法避免将错误从t线程提升到主线程。 我知道当Thread.abort\u on\u exception为false时,它工作正常。 但对于这个案子,我想保持真实。 可能吗 Thread.abort_on_exception = true 4.times{|i| Thread.new{ begin t = Thread.new{ sleep(10 - (Time.now.to_f % 10))
视窗7 我想执行这段代码,但我无法避免将错误从t线程提升到主线程。 我知道当Thread.abort\u on\u exception为false时,它工作正常。 但对于这个案子,我想保持真实。 可能吗
Thread.abort_on_exception = true
4.times{|i|
Thread.new{
begin
t = Thread.new{
sleep(10 - (Time.now.to_f % 10))
raise "same error"
}
t.abort_on_exception = false
t.join
rescue
print "do it again %d\n" % i
retry
end
}
}
sleep 60
欢迎提出任何建议。提前感谢。据我所知,将其设置为true基本上会使异常停止每个线程。如果这是正确的,那么在sleep 60声明之后拯救它可能是你能做的最好的事情:
begin
# your code here
rescue
puts "rescued"
end
医生们似乎很清楚,不:是的,这就是我为什么在这里问的原因。我明白了。现在我知道我所能做的就是重写Thread类或创建Thread类。非常感谢。