# Multithreading in ruby

**URL:** <https://www.ruby-forum.com/t/multithreading-in-ruby/145808>\
**Category:** Ruby\
**Created:** [August 14, 2008, 7:02am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808 "2008-08-14T07:02:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 7:02am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/1 "2008-08-14T07:02:03Z")

</div>

We people are new team for ruby. we trying to pput multithreading in our  
code. how is it possible? we want to read a message from queue and  
process that message parellaly and finally insert the message to the DB.  
Can you give me the Idea please

Regards  
Kaja M…A  
Trichy

---

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 7:13am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/2 "2008-08-14T07:13:09Z")

</div>

Threads and ProcessesMultithreading

Often the simplest way to do two things at once is by using _Ruby  
threads_.  
These are totally in-process, implemented within the Ruby interpreter.  
That  
makes the Ruby threads completely portable—there is no reliance on the  
operating system—but you don’t get certain benefits from having native  
threads. You may experience thread starvation (that’s where a  
low-priority  
thread doesn’t get a chance to run). If you manage to get your  
threadsdeadlocked, the whole process may grind to a halt. And if some  
thread happens to make a call to the operating system that takes a long  
time  
to complete, all threads will hang until the interpreter gets control  
back.  
However, don’t let these potential problems put you off—Ruby threads  
are a  
lightweight and efficient way to achieve parallelism in your code.  
Creating  
Ruby Threads

Creating a new thread is pretty straightforward. Here’s a simple code  
fragment that downloads a set of Web pages in parallel. For each request  
it’s given, the code creates a separate thread that handles the HTTP  
transaction.

require ‘net/http’

pages = %w( [www.rubycentral.com](http://www.rubycentral.com)  
[www.awl.com](http://www.awl.com)  
[www.pragmaticprogrammer.com](http://www.pragmaticprogrammer.com)  
)

threads = []

for page in pages  
threads \<\< Thread.new(page) { |myPage|

```
h = Net::HTTP.new(myPage, 80)
puts "Fetching: #{myPage}"
resp, data = h.get('/', nil )
puts "Got #{myPage}: #{resp.message}"

```

}  
end

threads.each { |aThread| aThread.join }

_produces:_

Fetching: [www.rubycentral.com](http://www.rubycentral.com)  
Fetching: [www.awl.com](http://www.awl.com)  
Fetching: [www.pragmaticprogrammer.com](http://www.pragmaticprogrammer.com)  
Got [www.rubycentral.com](http://www.rubycentral.com): OK  
Got [www.pragmaticprogrammer.com](http://www.pragmaticprogrammer.com): OK  
Got [www.awl.com](http://www.awl.com): OK

You may see the book came with installation of ruby, in /ruby/doc folder

On Thu, Aug 14, 2008 at 10:29 AM, Kaja M. \<  
removed\_email\_address@domain.invalid\> wrote:

> 

## –

Thanks and Regards  
Saurabh P.  
+91-9922907342  
skype: sorab\_pune  
yahoo & gtalk: saurabh.purnaye  
msn: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 7:14am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/3 "2008-08-14T07:14:08Z")

</div>

Kaja M. wrote:

> We people are new team for ruby. we trying to pput multithreading in our  
> code. how is it possible? we want to read a message from queue and  
> process that message parellaly and finally insert the message to the DB.  
> Can you give me the Idea please
> 
> Regards  
> Kaja M…A  
> Trichy

Use the Thread class. Just be aware that Ruby 1.8.x uses green threads.  
Ruby 1.9.x uses native threads.

---

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 7:21am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/4 "2008-08-14T07:21:56Z")

</div>

saurabh purnaye wrote:

> to complete, all threads will hang until the interpreter gets control back.  
> However, don’t let these potential problems put you off—Ruby threads are a  
> lightweight and efficient way to achieve parallelism in your code.

As has been discussed here many times before, neither Ruby 1.8.6 nor  
Ruby 1.9 run threads in parallel. The only implementations currently  
that have actual parallel threads are JRuby (for sure) and IronRuby (I  
think). So if having things process _actually_ in parallel is important  
for your app you may want to consider JRuby.

- Charlie

---

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 7:22am UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/5 "2008-08-14T07:22:32Z")

</div>

James D. wrote:

> Use the Thread class. Just be aware that Ruby 1.8.x uses green threads.  
> Ruby 1.9.x uses native threads.

Ruby 1.9 has native threads, but they do not run in parallel and Ruby is  
still in charge of scheduling them (or allowing the OS to schedule  
them).

- Charlie

---

<div class="post-metadata">

**Author:** ![kajamohaideen\_2003](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/eb9ed0/32.png) [@kajamohaideen\_2003](https://www.ruby-forum.com/u/kajamohaideen_2003)\
**Post date:** [August 14, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/multithreading-in-ruby/145808/6 "2008-08-14T12:33:00Z")

</div>

On Aug 14, 7:17 am, Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ruby 1.9 has native threads, but they do not run in parallel and Ruby is  
> still in charge of scheduling them (or allowing the OS to schedule them).

What are they planning for the future? Some time ago a joined research  
project between Tokyo University and Sun was annunced (  
[Common Ruby MVM API research kicked off](http://www.infoq.com/news/2008/02/ruby-mvm-research) ), did they  
release anything yet? Does anybody know if the guys developing the  
main Ruby implementations (MRI and Yarv) are planning something about  
this?  
Looks like both JRuby and Rubinius are sharing a common API for MVM  
and JRuby has support for concurrent threading too: I feel the lack  
for a common specification here, are we supposed to write Ruby code  
that won’t work on every Ruby implementation?
