# New to ruby, using threads

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800>\
**Category:** Ruby\
**Created:** [December 4, 2008, 3:05am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800 "2008-12-04T03:05:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 3:05am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/1 "2008-12-04T03:05:16Z")

</div>

I’m trying to schedule some task using ruby threads

This work great:

t = Thread.new do  
while true do  
puts “Starting new job at #{Time.now}”  
do\_something  
sleep 3  
end  
end  
t.join

But when the task take a long, like:

def do\_something  
puts “doing something at#{Time.now}”  
sleep(rand(3))  
end

I cant get the “Starting new job” message appears in regular intervals  
of 3 seconds. What I’m missing?

Thanks

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 3:40am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/2 "2008-12-04T03:40:37Z")

</div>

def do\_something  
puts “doing something at#{Time.now}”  
STDOUT.flush  
sleep(rand(8))  
end

t = Thread.new do  
while true do  
puts “Starting new job at #{Time.now}”  
do\_something  
sleep 3  
end  
end  
t.join

The problem is not the thread is the terminal where you are working on.  
Try using STDOUT.flush …

[te la baÃ±aste con lo del parangaricutirimicuaro]

-r.

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 3:50am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/3 "2008-12-04T03:50:40Z")

</div>

hi Poncho,  
what you want to do is probably have two parallel threads,  
so, try something like:

t1 = Thread.new do  
while true do  
puts “Starting new job at #{Time.now}”  
sleep 3  
end  
end

t2 = Thread.new do  
loop do  
do\_something  
end  
end

t1.join

2008/12/3 Poncho Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 4:42am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/4 "2008-12-04T04:42:50Z")

</div>

The main problem is you have a little misunderstand of thread.  
Based on your code, when the child thread is forked, there is nothing  
more for the main thread to do. So it joins the child thread  
immediately. And what is the child thread doing? Sleeping. So it looks  
like your program is sleeping.

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 10:02am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/5 "2008-12-04T10:02:38Z")

</div>

Poncho Q. wrote:

> I’m trying to schedule some task using ruby threads
> 
> This work great:
> 
> t = Thread.new do  
> while true do  
> puts “Starting new job at #{Time.now}”  
> do\_something  
> sleep 3  
> end  
> end  
> t.join
> 
> But when the task take a long, like:
> 
> def do\_something  
> puts “doing something at#{Time.now}”  
> sleep(rand(3))  
> end
> 
> I cant get the “Starting new job” message appears in regular intervals  
> of 3 seconds. What I’m missing?

It works just fine for me (ruby-1.8.6p114, ubuntu hardy). Note that  
rand(3) returns either 0, 1 or 2; it’s not going to be easy to see the  
delay varying.

It’s clearer if you print Time.now.to\_f

Starting new job at 1228381283.09741  
doing something at1228381283.12344  
Starting new job at 1228381288.13136 \<\< + 5  
doing something at1228381288.13152  
Starting new job at 1228381291.13536 \<\< + 3  
doing something at1228381291.13553  
Starting new job at 1228381296.14337 \<\< + 5  
doing something at1228381296.14353

Or change rand(3) to rand(10).

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 8:30am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/6 "2008-12-04T08:30:45Z")

</div>

On 04.12.2008 02:59, Poncho Q. wrote:

> end  
> of 3 seconds. What I’m missing?  
> You’re sleeping 3 seconds plus whatever rand(3) returns in your thread -  
> so the intervals have varying length. Basically in your code you do not  
> use threads properly because there is just one thread active (main  
> thread joins t and just sits there and waits for t to complete).

You probably rather wanted this

loop do  
puts “Starting new job at #{Time.now}”  
Thread.new do  
do\_something  
end  
sleep 3  
end

Note, since you have an infinite loop anyway you do not need to join the  
thread.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![barbacoa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@barbacoa](https://www.ruby-forum.com/u/barbacoa)\
**Post date:** [December 4, 2008, 10:08am UTC](https://www.ruby-forum.com/t/new-to-ruby-using-threads/154800/7 "2008-12-04T10:08:39Z")

</div>

Brian C. wrote:

> > I cant get the “Starting new job” message appears in regular intervals  
> > of 3 seconds. What I’m missing?
> 
> It works just fine for me (ruby-1.8.6p114, ubuntu hardy).

Of course, this assumes that what you’ve written is what you want:

- Start a single thread
- Within this thread:
  - call do\_something _and wait for it to finish_
  - sleep 3 seconds
  - loop around again

Clearly the total time taken for each iteration of this loop is the time  
taken in do\_something, plus 3 seconds, plus a small amount of overhead.
