Threads and loops

Hi, if I run this script:

loop{
puts “hello”
sleep 1
}

It outputs “hello” every second.
But if I place it in a separate thread:

Thread.new do
loop{
puts “hello”
sleep 1
}
end

it prints it only one time and then exits.
Why is that?

Emil S. wrote the following on 16.08.2007 13:20 :

it prints it only one time and then exits.
Why is that?

You don’t wait for the Thread to stop to exit your program.

On 8/16/07, Emil S. [email protected] wrote:

Thread.new do
loop{
puts “hello”
sleep 1
}
end

it prints it only one time and then exits.
Why is that?

When your program exits, the thread dies. Take a look at Thread.join
http://www.ruby-doc.org/core/classes/Thread.html#M000474