2010-05-19 125 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