JRuby on the other hand, is built upon the JVM. One can make use of "true" native threads which are able to run concurrently with you parent thread.
To visualize how this works check out this diagram:
Read more about the GIL here (from which I borrowed the above diagram).
Setup
Here are some quick instructions on how to setup JRuby on your machine so we can test out multi-threaded performance. The easiest way to install JRuby in my opinion is through Ruby Version Manager. RVM is essentially an easy way to manage multiple ruby versions (and their associated gems) on a single machine. For Linux/Unix/OSX instructions, see here. For windows go here.1- Install JRuby:
>rvm install jruby
2- Check that it's installed
> rvm list gemsets
3- rvm use <name of the jruby gemset>
> rvm use jruby-1.7.1
4- Test it out in irb (ruby's interactive console). You should see "java" as the RUBY_PLATFORM if things are installed properly:
Sample App
The following are two sample apps which showcase multi-threading in JRuby vs. Ruby 1.9 vs Ruby Enterprise Edition 1.8.7.JRuby Example (also showcasing how easy it is to call java code from Ruby):
Standard Ruby example:
To run the experiment:
1- Run in Jruby:
> ruby multi_thread_jruby.rb
2- Run in Ruby 1.9.3
> rvm install ruby-1.9.3-p286
> rvm use ruby-1.9.3-p286
> ruby multi_thread_ruby.rb
3- Run in Ruby 1.8.7
> rvm install ree-1.8.7-2012.02
> rvm use ree-1.8.7-2012.02
> ruby multi_thread_ruby.rb
Average completion time summary:
JRuby(1.7.1): 199.3ms
Ruby(1.9.3p286): 610.0ms
Ruby(ree-1.8.7-2012.02): 748.6ms
As we can see, JRuby is close to 4 times faster than Ruby 1.8 on my 4 core MacBook Air. Ruby 1.9 performs slightly better by being able to take advantage of time during which the process is I/O blocked. Overall, JRuby is the clear choice for maximizing multi-threaded performance.
Additional Reading:
More on calling Java from JRuby:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby
An interesting look at benchmarks of JRuby compared to other ruby versions. This shows that JRuby is not better in all scenarios:
http://etehtsea.me/the-great-ruby-shootout
#SeekDiversePerspectives


