# Threads and loops

**URL:** <https://www.ruby-forum.com/t/threads-and-loops/112214>\
**Category:** Ruby\
**Created:** [August 16, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/threads-and-loops/112214 "2007-08-16T13:20:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [August 16, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/threads-and-loops/112214/1 "2007-08-16T13:20:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [August 16, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/threads-and-loops/112214/2 "2007-08-16T13:26:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![emilsverige](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/77aa72/32.png) [@emilsverige](https://www.ruby-forum.com/u/emilsverige)\
**Post date:** [August 16, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/threads-and-loops/112214/3 "2007-08-16T13:58:53Z")

</div>

On 8/16/07, Emil S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.ruby-doc.org/core/classes/Thread.html#M000474)
