Multithreading problem

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
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
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.

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?

Venkat B. wrote:

while start < finish
puts "running thread2"
end

regards,
Venkat B.

What is the error message?

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

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.

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.

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.