# Ruby And Threads

**URL:** <https://www.ruby-forum.com/t/ruby-and-threads/127921>\
**Category:** Ruby\
**Created:** [February 2, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921 "2008-02-02T18:06:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![magician](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@magician](https://www.ruby-forum.com/u/magician)\
**Post date:** [February 2, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921/1 "2008-02-02T18:06:24Z")

</div>

I need make my application execute 5 operations in the same time, for  
that i make the class Operation and extends to Thread, make method do  
and its work fine buts when i start the threads they don’t run all in  
the same time.  
To run i make

t1 = Operation.new  
t2 = Operation.new  
t3 = Operation.new  
t4 = Operation.new  
t5 = Operation.new

and

t1.join  
t2.join  
…  
I’m doing something wrong ? Why the threads don’t run all the same time?

---

<div class="post-metadata">

**Author:** ![magician](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@magician](https://www.ruby-forum.com/u/magician)\
**Post date:** [February 2, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921/2 "2008-02-02T18:58:05Z")

</div>

On 02/02/2008, Magician W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> t1 = Operation.new

I don’t know what an Operation class is …

---

<div class="post-metadata">

**Author:** ![magician](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@magician](https://www.ruby-forum.com/u/magician)\
**Post date:** [February 2, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921/3 "2008-02-02T19:07:24Z")

</div>

Thomas P. wrote:

> On 02/02/2008, Magician W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > t1 = Operation.new
> 
> I don’t know what an Operation class is …

Is one class extended to the Thread class and whith one specific  
operation.

---

<div class="post-metadata">

**Author:** ![magician](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@magician](https://www.ruby-forum.com/u/magician)\
**Post date:** [February 2, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921/4 "2008-02-02T19:35:18Z")

</div>

Magician W. wrote:

> Thomas P. wrote:
> 
> > On 02/02/2008, Magician W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > t1 = Operation.new
> > 
> > I don’t know what an Operation class is …
> 
> Is one class extended to the Thread class and whith one specific  
> operation.  
> Why would you do that?

first of all, reember to call super if you do initialization, second why  
don’t you just provide Thread.new with block?

anyway if you want usefull help, explain your question better or paste  
code to pastie.caboo.se 😛

---

<div class="post-metadata">

**Author:** ![magician](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@magician](https://www.ruby-forum.com/u/magician)\
**Post date:** [February 3, 2008, 12:41am UTC](https://www.ruby-forum.com/t/ruby-and-threads/127921/5 "2008-02-03T00:41:51Z")

</div>

Marcin R. wrote:

> Magician W. wrote:
> 
> > Is one class extended to the Thread class and whith one specific  
> > operation.  
> > Why would you do that?  
> > first of all, reember to call super if you do initialization, second why  
> > don’t you just provide Thread.new with block?

There might be good reasons to do that. I often subclass Thread to  
give each thread an API for status and interchange, other than just  
shared variables. That way you can include the required synchronization  
inside the API.

That said, does the OP know that Ruby’s “green threads” will only ever  
schedule one thread at a time? Even on a multi-core/multi-CPU box?  
If each thread completes within its timeslice, it will appear that  
they’re running sequentially.

Clifford H…
