# Threads terminating

**URL:** <https://www.ruby-forum.com/t/threads-terminating/177541>\
**Category:** Ruby\
**Created:** [October 20, 2009, 12:50am UTC](https://www.ruby-forum.com/t/threads-terminating/177541 "2009-10-20T00:50:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pawel](https://www.ruby-forum.com/u/pawel)\
**Post date:** [October 20, 2009, 12:50am UTC](https://www.ruby-forum.com/t/threads-terminating/177541/1 "2009-10-20T00:50:16Z")

</div>

Why this simple program give no output, and terminates immidiatly  
after its launched? There should be thread running and giving lots of  
‘lala’, am I right?  
class Bar  
def initialize

end  
def foo  
Thread.new do  
while true  
puts “lala\n”  
end  
end  
end  
end

b=Bar.new  
b.foo

---

<div class="post-metadata">

**Author:** ![pawel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pawel](https://www.ruby-forum.com/u/pawel)\
**Post date:** [October 20, 2009, 12:50am UTC](https://www.ruby-forum.com/t/threads-terminating/177541/2 "2009-10-20T00:50:17Z")

</div>

El Martes, 20 de Octubre de 2009, pawel  
escribió:\> puts “lala\n”

> ```
> end
> end
> 
> ```
> 
> end  
> end
> 
> b=Bar.new  
> b.foo

When you create a thread, the main threads continues. In you case the  
main  
thread terminates after calling “Thread.new” so the program exists.

If you want to _wait_ all the created threads to finish their work, then  
you  
must “join” them:

my\_thread = Thread.new do … end  
do other stuff in main thread  
my\_thread.join

---

<div class="post-metadata">

**Author:** ![pawel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pawel](https://www.ruby-forum.com/u/pawel)\
**Post date:** [October 20, 2009, 1:03am UTC](https://www.ruby-forum.com/t/threads-terminating/177541/3 "2009-10-20T01:03:10Z")

</div>

On 20 Oct, 2009, at 1:05 AM, pawel wrote:

> Why this simple program give no output, and terminates immidiatly  
> after its launched? There should be thread running and giving lots of  
> ‘lala’, am I right?  
> class Bar  
> def initialize
> 
> end  
> def foo  
> Thread.new do

try changing this to a = Thread.new

> ```
> while true
> puts "lala\n"
> end
> 
> ```
> 
> end

then add an a.join here

> end  
> end
> 
> b=Bar.new  
> b.foo

Someone else would have to explain why that happens. Don’t know enough  
about threads (let alone ruby’s) to explain that.

---

<div class="post-metadata">

**Author:** ![pawel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pawel](https://www.ruby-forum.com/u/pawel)\
**Post date:** [October 20, 2009, 3:46am UTC](https://www.ruby-forum.com/t/threads-terminating/177541/4 "2009-10-20T03:46:32Z")

</div>

pawel wrote:

> ```
> puts "lala\n"
> end
> end
> 
> ```
> 
> end  
> end
> 
> b=Bar.new  
> b.foo

Hi Pawel, here is one example, see if you can follow it.

class Bar  
def initialize( msg )  
@msg = msg  
end  
def foo  
t\_exit = Time.now.to\_i + 3  
while true  
puts @msg  
break if Time.now.to\_i \> t\_exit  
sleep 0.5  
end  
end  
end

b1 = Thread.new{ Bar.new(“x”).foo }  
b2 = Thread.new{ Bar.new(“o”).foo }

# here main thread waits for thread b1 and b2 to exit

b1.join  
b2.join

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)  
Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![pawel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pawel](https://www.ruby-forum.com/u/pawel)\
**Post date:** [October 20, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/threads-terminating/177541/5 "2009-10-20T22:05:27Z")

</div>

On 20.10.2009 00:34, Patrick O. wrote:

> > ```
> > Thread.new do
> > 
> > ```
> > 
> > end  
> > end
> > 
> > b=Bar.new  
> > b.foo
> 
> Someone else would have to explain why that happens. Don’t know enough  
> about threads (let alone ruby’s) to explain that.

All threads other than main are demon threads in Ruby, i.e. as has been  
explained they do not keep the process alive.

Kind regards

robert
