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' } 

为什么如果我运行我没有输出? (没有睡觉等待?)

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

 foo { sleep 1 puts 'test' }.join