我正在运行几个线程,偶尔会出现错误。
我是否可以将整个线程放入开始/救援/结束结构中,所以如果出现错误,我可以捕获该错误,然后只需重置该线程即可。
类似以下内容:
$threads = {}
numbers = 1 2 3 4 5
def start_threads(numbers)
numbers.each do |number|
begin
$threads[number] = Thread.new {(object.new.method)}
rescue
reset_thread(number)
end
end
end
def reset_thread(number)
$threads[number].join
$threads[number].exit
$threads[number].delete
$threads[number] = Thread.new {(object.new.method)}
end