Forum: Ruby calling methods of subclassed thread

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
121c13a2b43ffd45bdaa87843605604c?d=identicon&s=25 Christopher Dancy (christoforever)
on 2008-10-08 15:26
(Received via mailing list)
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

        ## neither of the previous methods execute.
        puts "here is the class " + @callers[local_num].class.to_s #
spits  out NilClass. Shouldent this be Thread?
      end
    end
  end

  def finish
    puts "And im spent...Simulation is over"
  end
end
start = Start.new
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-10-08 16:35
(Received via mailing list)
2008/10/8 christoforever@gmail.com <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.

Chris, add

  Thread.abort_on_exception = true

before starting your code to see more clearly what's going on.
Remember that Thread.new immediately starts the thread's code, so in
your case the code runs before the assignment to the @callers array.

BTW: if you want to access the current thread you have to use
Thread.current.

Regards,
Pit
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-08 16:57
(Received via mailing list)
Attachment: thread-test.rb (506 Bytes)
2008/10/8 christoforever@gmail.com <christoforever@gmail.com>:
>  def initialize(num='')
>  end
>  def start_simulation
>        @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
121c13a2b43ffd45bdaa87843605604c?d=identicon&s=25 Christopher Dancy (christoforever)
on 2008-10-08 19:30
(Received via mailing list)
On Oct 8, 10:32 am, Pit Capitain <pit.capit...@gmail.com> wrote:
> before starting your code to see more clearly what's going on.
> Remember that Thread.new immediately starts the thread's code, so in
> your case the code runs before the assignment to the @callers array.
>
> BTW: if you want to access the current thread you have to use Thread.current.
>
> Regards,
> Pit if

I assumed thats what was going on but just wanted a second opinion.
Thanks for the help. But why then if I call self.time_to_die ( bad
name for method i know) why does that not execute?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-09 09:29
(Received via mailing list)
2008/10/8 christoforever@gmail.com <christoforever@gmail.com>:
>>
> Thanks for the help. But why then if I call self.time_to_die ( bad
> name for method i know) why does that not execute?

It executes and throws but you do not see the exception because
Thread.abort_on_exception is likely false.

robert
This topic is locked and can not be replied to.