# Threads and puts

**URL:** <https://rubytalk.org/t/threads-and-puts/39945>\
**Category:** ruby-talk\
**Created:** [16 August 2007 10:51 UTC](https://rubytalk.org/t/threads-and-puts/39945 "2007-08-16T10:51:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@Emil](https://rubytalk.org/u/Emil)\
**Post date:** [16 August 2007 10:51 UTC](https://rubytalk.org/t/threads-and-puts/39945/1 "2007-08-16T10:51:02Z")

</div>

Hi, can anyone explain why this script:

Thread.new do  
&nbsp;&nbsp;3.times{  
&nbsp;&nbsp;&nbsp;&nbsp;puts "hello"  
&nbsp;&nbsp;}  
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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tom\_Werner](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@Tom\_Werner](https://rubytalk.org/u/Tom_Werner)\
**Post date:** [16 August 2007 17:44 UTC](https://rubytalk.org/t/threads-and-puts/39945/2 "2007-08-16T17:44:04Z")

</div>

Emil Sandin wrote:

> Hi, can anyone explain why this script:
> 
> Thread.new do  
> &nbsp;&nbsp;3.times{  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "hello"  
> &nbsp;&nbsp;}  
> 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?  
> &nbsp;&nbsp;  
> 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  
&nbsp;&nbsp;3.times{  
&nbsp;&nbsp;&nbsp;&nbsp;puts "hello"  
&nbsp;&nbsp;}  
end

t.join

Hope this helps.

Tom

> **···**
>
> --  
> \* Libraries:  
> &nbsp;&nbsp;&nbsp;&nbsp;Chronic (chronic.rubyforge.org)  
> &nbsp;&nbsp;&nbsp;&nbsp;God (god.rubyforge.org)  
> \* Site:  
> &nbsp;&nbsp;&nbsp;&nbsp;rubyisawesome.com

---

<div class="post-metadata">

**Author:** ![Emil](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@Emil](https://rubytalk.org/u/Emil)\
**Post date:** [16 August 2007 20:10 UTC](https://rubytalk.org/t/threads-and-puts/39945/3 "2007-08-16T20:10:47Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [17 August 2007 04:37 UTC](https://rubytalk.org/t/threads-and-puts/39945/4 "2007-08-17T04:37:59Z")

</div>

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

> **···**
>
> --  
> Giles Bowkett
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)
