Multithreading problem

V

Venkat Bagam

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

7stud --

Venkat said:
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?
 
J

Justin Collins

Venkat said:
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 Bagam

What is the error message?
 
V

Venkat Bagam

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

7stud --

Venkat said:
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.
 
7

7stud --

7stud said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top