# Multithreading problem

**URL:** <https://www.ruby-forum.com/t/multithreading-problem/130315>\
**Category:** Ruby\
**Created:** [February 27, 2008, 9:16am UTC](https://www.ruby-forum.com/t/multithreading-problem/130315 "2008-02-27T09:16:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 27, 2008, 9:16am UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/1 "2008-02-27T09:16:16Z")

</div>

Hi All,

```
       I am pretty much a starter in multi-threading. In one of my

```

application, I needed to run three threads along each other. Here is my  
code

require ‘rubygems’  
require ‘mechanize’  
require ‘fileutils’  
require ‘timeout’

agent = WWW::Mechanize.new

start\_time = Time.now  
threads = []

threads \<\< Thread.new do  
puts “running thread1”  
start = 0  
count = 10  
finish = 30  
while start \< finish  
page\_links = Array.new  
parent\_url = “[http://www.rknowsys.com](http://www.rknowsys.com)”  
page = agent.get(parent\_url)  
page = nil  
start += count  
end  
puts “thread1 completed” if start == finish  
end

threads \<\< Thread.new do  
puts “running thread2”  
start = 30  
count = 10  
finish = 60  
while start \< finish  
page\_links = Array.new  
parent\_url = “[http://www.rknowsys.com](http://www.rknowsys.com)”  
page = agent.get(parent\_url)  
page = nil  
start += count  
raise “stopped bcaz start \> 40” if start \> 40  
end  
puts “thread2 completed” if start == finish  
end

threads.each do |thread|  
begin  
thread.join  
rescue RuntimeError =\> e  
puts e.message  
end  
end

end\_time = Time.now  
interval = end\_time - start\_time  
puts interval

the above code prints the error message, at the time of each thread join  
and stops.  
But, How do I deal with such a scenario that the thread2 continues to  
run again setting its local variable start = 30 ?

Any help appreciated.

regards,  
Venkat B.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 27, 2008, 10:46am UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/2 "2008-02-27T10:46:59Z")

</div>

Venkat B. wrote:

> the above code prints the error message, at the time of each thread join  
> and stops.  
> But, How do I deal with such a scenario that the thread2 continues to  
> run again

How would thread2 be made to run again?

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 27, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/3 "2008-02-27T20:02:53Z")

</div>

Venkat B. wrote:

> ```
> while start < finish
> puts "running thread2"
> end
> 
> ```
> 
> regards,  
> Venkat B.

What is the error message?

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 27, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/4 "2008-02-27T21:48:48Z")

</div>

Justin C. wrote:

> Venkat B. wrote:
> 
> > ```
> > while start < finish
> > puts "running thread2"
> > end
> > 
> > ```
> > 
> > regards,  
> > Venkat B.
> 
> What is the error message?

raise “stopped bcaz start \> 40” if start \> 40

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 28, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/5 "2008-02-28T14:46:09Z")

</div>

threads.each do |thread|  
begin  
thread.join  
rescue RuntimeError =\> e  
puts e.message  
end  
end

right now i was displaying a customized error message “stopped bcaz  
start \> 40” in rescue. But if I want to re run the thread rather than  
printing the message, is it possible to do? I tried “thread.run”,  
“thread.wakeup” etc. but it shits something like “undefined method run  
called for dead thread” etc…

any guess?

regards,  
Venkat B.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 28, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/6 "2008-02-28T18:34:46Z")

</div>

7stud – wrote:

> Threads are created, they run, and then they cease to exist. You can’t  
> rerun a thread.

However, you can run a new thread and have it use some values that a  
previous thread inserted into a global variable.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [February 28, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/multithreading-problem/130315/7 "2008-02-28T18:32:28Z")

</div>

Venkat B. wrote:

> threads.each do |thread|  
> begin  
> thread.join  
> rescue RuntimeError =\> e  
> puts e.message  
> end  
> end

> But if I want to re run the thread

Threads are created, they run, and then they cease to exist. You can’t  
rerun a thread.
