# Calling methods of subclassed thread

**URL:** <https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433>\
**Category:** Ruby\
**Created:** [October 8, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433 "2008-10-08T15:26:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [October 8, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433/1 "2008-10-08T15:26:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [October 8, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433/2 "2008-10-08T16:35:29Z")

</div>

2008/10/8 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [October 8, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433/3 "2008-10-08T16:57:52Z")

</div>

2008/10/8 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [October 9, 2008, 9:29am UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433/4 "2008-10-09T09:29:08Z")

</div>

2008/10/8 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![christoforever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@christoforever](https://www.ruby-forum.com/u/christoforever)\
**Post date:** [October 8, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/calling-methods-of-subclassed-thread/150433/5 "2008-10-08T19:30:34Z")

</div>

On Oct 8, 10:32 am, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?
