Ruby多后台线程

Ruby多后台线程,ruby,Ruby,我需要在有超时的线程池中运行多个后台线程。 该计划大致如下: #!/usr/bin/env ruby require 'thread' def foo(&block) bar(block) end def bar(block) Thread.abort_on_exception=true @main = Thread.new { block.call } end foo { sleep 1 puts 'test' } 为什么如果我运行它,我就没有输出?(并

我需要在有超时的线程池中运行多个后台线程。 该计划大致如下:

    #!/usr/bin/env ruby

require 'thread'

def foo(&block)
  bar(block)
end

def bar(block)
  Thread.abort_on_exception=true
  @main = Thread.new { block.call }
end


foo {
sleep 1
puts 'test'
}

为什么如果我运行它,我就没有输出?(并且没有睡眠等待?

当主线程结束时,程序结束。您必须等待由
bar
使用
join
创建的线程:

foo {
  sleep 1
  puts 'test'
}.join
试试这个工作