# Threads and puts

**URL:** <https://www.ruby-forum.com/t/threads-and-puts/112211>\
**Category:** Ruby\
**Created:** [August 16, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/threads-and-puts/112211 "2007-08-16T12:51:05Z")\
**Posts on this page:** 4\
**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, 12:51pm UTC](https://www.ruby-forum.com/t/threads-and-puts/112211/1 "2007-08-16T12:51:05Z")

</div>

Hi, can anyone explain why this script:

Thread.new do  
3.times{  
puts “hello”  
}  
end

Gives the folllowing output:  
“hello”

I expect it to print “hello” three times, just as it would if I hadn’t  
put it inside a thread. Do I need to flush the output or something like  
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, 7:46pm UTC](https://www.ruby-forum.com/t/threads-and-puts/112211/2 "2007-08-16T19:46:50Z")

</div>

Emil S. wrote:

> I expect it to print “hello” three times, just as it would if I hadn’t  
> put it inside a thread. Do I need to flush the output or something like  
> that?

It’s a bit surprising that it outputs anything at all! If the above is  
your entire program, then when the program exits, the thread will be  
killed. On my system, that means no output at all. In order to wait for  
the thread to complete (before the program exits), you need to call  
‘join’ on it like so:

t = Thread.new do  
3.times{  
puts “hello”  
}  
end

t.join

Hope this helps.

Tom

---

<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, 10:10pm UTC](https://www.ruby-forum.com/t/threads-and-puts/112211/3 "2007-08-16T22:10:50Z")

</div>

Thank you all, this makes it all much more understandable!

---

<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 17, 2007, 6:38am UTC](https://www.ruby-forum.com/t/threads-and-puts/112211/4 "2007-08-17T06:38:56Z")

</div>

Just for perspective, on my system, this prints “hello” three times,  
as (incorrectly) expected.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
