From: Robert Klemme Date: 2008-10-08T23:54:18+09:00 Subject: Re: calling methods of subclassed thread ------=_Part_73697_30221136.1223477784209 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 2008/10/8 christoforever@gmail.com : > As you will see at the bottom of this code snippet, i've commented out > where the problem is. There are no errors but simply the methods never > execute. I cant figure out for the life of me why that is. I've tried > a few different things but nothing is working. I would love to know > what I'm doing wrong or maybe ruby cant do what im asking of it... Im > not sure either way and would love some feedback. Thanks in advance > > Chris Dancy > > class MyThread < Thread > def initialize(num='') > super > puts "Thread #{num} has been created" > @identity = num > @attempts = 0 > @time_waiting = Time.new > @total_time = Time.new > end > def time_to_die > puts "Thread #{@identity} is dying now..." > self.kill > end > end > > class Start > def initialize > @NUMBER_OF_PEOPLE = 10 > @callers = Array.new(@NUMBER_OF_PEOPLE) > @phone_held = false > self.start_simulation > self.finish > end > def start_simulation > puts "Starting Simulation..." > @NUMBER_OF_PEOPLE.times do |num| > puts " " + num.to_s > > @callers[num] = MyThread.new(num) do |local_num| > > ## I want to call this executing MyThread method time_to_die > but its not working > ## this does not work, but gives no errors > > @callers[local_num].time_to_die > self.time_to_die self is an instance of Start and not MyThread here. > ## neither of the previous methods execute. > puts "here is the class " + @callers[local_num].class.to_s # > spits out NilClass. Shouldent this be Thread? You are dealing with concurrency. Which means, at this point in time the assignment in the starting thread might not have happened yet. You can play with the attached file to help gain understanding. > end > end > end > > def finish > puts "And im spent...Simulation is over" > end > end > start = Start.new I am not sure what you are trying to achieve but the design looks a little awkward to me. Chances are that using another approach might yield better results. Cheers robert -- remember.guy do |as, often| as.you_can - without end ------=_Part_73697_30221136.1223477784209 Content-Type: application/x-ruby; name=thread-test.rb Content-Transfer-Encoding: base64 X-Attachment-Id: f_fm232tk90 Content-Disposition: attachment; filename=thread-test.rb IyEvYmluL2VudiBydWJ5CgpkZWYgdHByaW50KGEpCiAgcHJpbnRmICIlLTEwcCAlc1xuIiwgVGhy ZWFkLmN1cnJlbnQsIGEKZW5kCgpjbGFzcyBNeVRocmVhZFdyb25nIDwgVGhyZWFkCiAgZGVmIGlu aXRpYWxpemUoKQogICAgdHByaW50ICJpbml0IDEgI3tzZWxmLmNsYXNzfSIKICAgIHN1cGVyCiAg ICB0cHJpbnQgImluaXQgMiAje3NlbGYuY2xhc3N9IgogIGVuZAplbmQKCmNsYXNzIE15VGhyZWFk UmlnaHQgPCBUaHJlYWQKICBkZWYgaW5pdGlhbGl6ZSgmYikKICAgIHRwcmludCAiaW5pdCAxICN7 c2VsZi5jbGFzc30iCiAgICBzdXBlcgogICAgdHByaW50ICJpbml0IDIgI3tzZWxmLmNsYXNzfSIK ICBlbmQKZW5kCgp0aHJlYWRzID0gW1RocmVhZCwgTXlUaHJlYWRXcm9uZywgTXlUaHJlYWRSaWdo dF0ubWFwIGRvIHxjbHwKICBjbC5uZXcgZG8KICAgIHRwcmludCAicnVuICN7VGhyZWFkLmN1cnJl bnQuY2xhc3N9IgogIGVuZAplbmQKCnRocmVhZHMuZWFjaCB7fHRofCB0aC5qb2lufQo= ------=_Part_73697_30221136.1223477784209--