Ruby:是否可以将整个线程包装在异常处理程序中?

时间:2019-04-30 18:53:16

标签: ruby multithreading exception rescue

我正在运行几个线程,偶尔会出现错误。

我是否可以将整个线程放入开始/救援/结束结构中,所以如果出现错误,我可以捕获该错误,然后只需重置该线程即可。

类似以下内容:

$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

0 个答案:

没有答案