The following code throw messages without putting them with line break.
threads = []
counter = 1000
counter.times do
threads << Thread.new do
puts "This is a line."
sleep 1
end
end
threads.each {|t| t.join}
The result is
This is a line. This is a line
This is a line.
This is a line.
and so on...
Is there anyway to print the result line in a tidier way?
putsworks internally by printing its argument, then by printing a newline. Sometimes, the thread gets interrupted between the two operations, resulting in the behavior you’re seeing. You could instead say:…which would write the entire string — including newline — in a single operation.