# Simple threading

**URL:** <https://www.ruby-forum.com/t/simple-threading/169836>\
**Category:** Ruby\
**Created:** [June 9, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/simple-threading/169836 "2009-06-09T13:07:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/simple-threading/169836/1 "2009-06-09T13:07:08Z")

</div>

Hi all,  
I wanted to write a program that request stock quotes say every second.  
So I  
started to write a thread like this:

require ‘rubygems’  
require ‘yahoofinance’

thread=Thread.new do  
YahooFinance::get\_historical\_quotes\_days( ‘goog’, 10 ) do |row|  
puts “YHOO,#{row.join(’,’)}”  
end  
end

When I run this I don’t get any results. Which is really weird. Got all  
the  
gems and so on.  
But when I do the same code run in IRB it works! without any surprises.  
I  
don’t get it. Really.

Thanks for any help.

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/simple-threading/169836/2 "2009-06-09T13:24:45Z")

</div>

On Tue, Jun 9, 2009 at 4:36 PM, Christoph  
Jasinski[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

Not sure, but adding ‘thread.join’ to the end might help.

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/simple-threading/169836/3 "2009-06-09T14:29:53Z")

</div>

Thanks,  
it works; I don’t know why, but it works

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [June 9, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/simple-threading/169836/4 "2009-06-09T15:21:28Z")

</div>

On Jun 9, 2009, at 7:28 AM, Christoph J. wrote:

> Thanks,  
> it works; I don’t know why, but it works

I know why it work. 🙂 Let me explain:

1. Your code splits into two threads (the main Thread Ruby started  
your program in and the one you created to talk to YahooFinance in)
2. After you have the two Threads, they both begin working (trading  
off time)
3. Your YahooFinance Thread probably starts talking over the network
4. However, your main Thread runs off the end of your file and when  
that happens, Ruby exits (killing the unfinished YahooFinance Thread)

Thus, Vikhyat had you add a line that asked the main Thread to join()  
or wait on the YahooFinance Thread to finish. That call won’t return  
until the second Thread ends, so Ruby will wait to exit.

Of course, that means the second Thread doesn’t really do anything for  
us in this case. But if you are launching a bunch of them or  
continuing to work in the main Thread, there will be benefits.

Hope that helps.

James Edward G. II
