Reputation: 8424
Thread.abort_on_exception = true
threads = 4.times.map do |number|
Thread.new(number) do |i|
raise "Boom!" if i == 1
print "#{i}\n"
end
end
puts "Waiting"
threads.each {|t| t.join }
puts "Done"
produces 0, 2 and then the error. I don't understand why it outputs 2 every time when it makes sense to output only 0 and then exit?
Upvotes: 0
Views: 50
Reputation: 6310
It's a scheduling issue. The threads don't truly run in parallel. Instead the Ruby interpreter continuously switches between the executing threads.
In this case, thread 1 raises an exception which must be handled. This presumably takes enough time to allow thread 2 to execute.
Try putting in a sleep 0.1
between the raise
and the print
line, that should result in no output at all.
Upvotes: 2