2010-05-19 127 views
2

我需要在超时的线程池中运行多个后台线程。 的方案是这样的: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' 
} 

为什么,如果我运行我没有得到任何输出? (并且没有睡眠等待?)

回答

3

程序在主线程结束时结束。你必须等待使用joinbar创建的线程上:

foo { 
    sleep 1 
    puts 'test' 
}.join