How can I return a value from a thread in Ruby?

后端 未结 5 1113
清歌不尽
清歌不尽 2021-02-03 19:08

If I have the following code :

threads = []
(1..5).each do |i|
  threads << Thread.new { `process x#{i}.bin` } 
end
threads.each do |t|
  t.join
  # i\'d l         


        
5条回答
  •  猫巷女王i
    2021-02-03 19:54

    Just use Thread#value:

    threads = (1..5).collect do |i|
      Thread.new { `echo x#{i}.bin` }
    end
    threads.each do |t|
      puts t.value
    end
    

提交回复
热议问题