# Subclassing Thread?

**URL:** <https://www.ruby-forum.com/t/subclassing-thread/115913>\
**Category:** Ruby\
**Created:** [September 30, 2007, 8:55am UTC](https://www.ruby-forum.com/t/subclassing-thread/115913 "2007-09-30T08:55:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 8:55am UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/1 "2007-09-30T08:55:17Z")

</div>

Is it possible to launch a Ruby thread without passing a block to  
Thread.new?

Those familiar with threading in Java will know that you can write a  
class that is a subclass of Thread, and then put the thread’s code into  
‘public static void run,’ which gets executed once thread.start is  
called. Can something similar to this be done in Ruby?

Basically, I want to write a Ruby class that is subclass of thread, and  
whose ‘initialize’ method is called when it is created. Can it be done?

Thanks,  
Stephen

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 9:16am UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/2 "2007-09-30T09:16:12Z")

</div>

On Sep 30, 1:55 am, Stephen W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks, Stephen
> 
> Posted viahttp://www.ruby-forum.com/.

This seems to work:

class MyThread \< Thread  
def initialize  
super(“purple monkey dishwasher”) {|str| puts “She said,  
‘#{str}.’”}  
end  
end

> MyThread.new  
> She said, ‘purple monkey dishwasher.’  
> =\> #\<MyThread:0x2aaaac528d40 run\>

Adjust arguments to #initialize as needed.

Jeremy

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/3 "2007-09-30T17:13:07Z")

</div>

Stephen W. wrote:

> Those familiar with threading in Java will know that you can write a  
> class that is a subclass of Thread, and then put the thread’s code into  
> ‘public static void run,’ which gets executed once thread.start is  
> called. Can something similar to this be done in Ruby?

The following simulates calling run to start a thread. It stops the  
thread as soon as it starts, and then restarts the thread when needed:

class MyThread \< Thread  
def initialize(x, y)  
super(x, y) do |val1, val2|  
Thread.stop  
puts “In thread…”  
puts val1, val2  
puts  
end  
end  
end

t = MyThread.new(“hello”, “world”)  
puts “main program”  
puts  
sleep(2)

t.run

t.join  
sleep(2)  
puts “main program ending…”

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/4 "2007-09-30T18:11:18Z")

</div>

> > Those familiar with threading in Java will know that you can write a  
> > class that is a subclass of Thread, and then put the thread’s code into  
> > ‘public static void run,’ which gets executed once thread.start is  
> > called. Can something similar to this be done in Ruby?

The usual question is, why would you want that?  
A Thread, by its nature, is something that should be running.  
Especially delaying the start means… there’s no underlying thread yet!

And I really love the syntax of Ruby:  
Thread.new {  
// thread’s code  
}

What are you missing?

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/5 "2007-09-30T20:02:30Z")

</div>

On Sep 30, 2007, at 12:10 PM, Kero wrote:

> And I really love the syntax of Ruby:  
> Thread.new {  
> // thread’s code  
> }

Quick thread question:

Does a thread need to be started with x.join, or will it start  
automatically?

Ari  
--------------------------------------------|  
If you’re not living on the edge,  
then you’re just wasting space.

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/6 "2007-09-30T20:12:47Z")

</div>

Ari B. wrote:

> Quick thread question:
> 
> Does a thread need to be started with x.join, or will it start  
> automatically?

As this thread highlights, there doesn’t seem to be a way to create a  
thread in Ruby without starting it. So, the answer to your question  
is: no, x.join does not start a thread. In fact, join() stops the  
thread in which the join() statement appears.

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 1, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/7 "2007-10-01T16:08:34Z")

</div>

2007/9/30, Stephen W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Kero wrote:
> 
> > The usual question is, why would you want that?  
> > A Thread, by its nature, is something that should be running.  
> > Especially delaying the start means… there’s no underlying thread yet!
> 
> My motivation for wanting the thread in a separate class is simply  
> convenience. The class will be very large… several pages of code at  
> least… so putting it into a block would be a little unwieldy.

Personally I always found the Java way to inherit java.lang.Thread  
inferior to using Runnable because the latter is much more flexible.  
So from my point of view you are asking for the worse alternative. 🙂

Btw, if your class is multiple screen pages this _may_ be an  
indication that you should do some refactoring and distribute  
functionality across multiple classes.

> Thanks very much for your suggestions. I’ve found another method that  
> seems to work well also. Rather than subclassing Thread, I have written  
> the class (call it “User”) as a normal class and done this:
> 
> Thread.new(args){|args| User.new(args)}
> 
> That seems to accomplish what I’m going for.

That’s also what I’d do.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [September 30, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/subclassing-thread/115913/8 "2007-09-30T19:14:18Z")

</div>

Kero wrote:

> The usual question is, why would you want that?  
> A Thread, by its nature, is something that should be running.  
> Especially delaying the start means… there’s no underlying thread yet!

My motivation for wanting the thread in a separate class is simply  
convenience. The class will be very large… several pages of code at  
least… so putting it into a block would be a little unwieldy.

Thanks very much for your suggestions. I’ve found another method that  
seems to work well also. Rather than subclassing Thread, I have written  
the class (call it “User”) as a normal class and done this:

Thread.new(args){|args| User.new(args)}

That seems to accomplish what I’m going for.

Thanks for your help!
