Concurrency Basics
Thread Join
A thread runs work independently, and join waits for it to finish.
Thread Join
thread_join.rb
work_units =
result = 0
worker = Thread.new do
subtotal = work_units * 10
result = subtotal + 1
end
worker.join
puts "work_units=#{work_units}"
puts "result=#{result}"
puts "worker_done=#{!worker.alive?}"
work_units =
result = 0
worker = Thread.new do
subtotal = work_units * 10
result = subtotal + 1
end
worker.join
puts "work_units=#{work_units}"
puts "result=#{result}"
puts "worker_done=#{!worker.alive?}"
work_units =
result = 0
worker = Thread.new do
subtotal = work_units * 10
result = subtotal + 1
end
worker.join
puts "work_units=#{work_units}"
puts "result=#{result}"
puts "worker_done=#{!worker.alive?}"
thread join
`Thread.new` starts a block on another Ruby thread. `join` keeps the main code from reading the result too early.