SlideShare a Scribd company logo
JRuby
Concurrency
Nick Sieger
EMRubyConf @ RailsConf 2011
Mental
Model




         http://www.flickr.com/photos/solar_decathlon/4524503892/
http://twitter.com/RoflscaleTips/status/61135427813384192
Uncertainty
/* ruby/struct.c */
static VALUE rb_struct_equal(VALUE s, VALUE s2) {
    /* ... */

    if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
        rb_bug("inconsistent struct"); /* should never happen */
    }

    /* ... */
}




                                    O RLY?!
How could this code fail?
class ExpensiveToCreate
  def self.instance
    @instance ||= ExpensiveToCreate.new
  end
end
How could this code fail?
class ExpensiveToCreate
  def self.instance
    unless defined?(@instance)
      @instance = ExpensiveToCreate.new
    end
  end
end
How could this code fail?
     class ExpensiveToCreate
       def self.instance
T2       unless defined?(@instance)
           @instance = ExpensiveToCreate.allocate
T1         @instance.initialize # big pause here
           @instance
         end
       end
     end
Avoid shared mutable state,
     lazy initialization

              MutableConstant.merge! :key => value

class MemoryCache
  def self.cache
    @cache ||= {}     $global_lock.do_something!
  end
end
Difficult to observe
with green threads
data = []
M.times do |m|
  Thread.new do
    N.times do |n|
      data << m * n
    end
  end
end
OK          NOT OK

             instance vars
method def
                String
 class def
                Array
class vars
                Hash
class WorkerTask
  def run
    @thread = Thread.new do
      50.times do
        digest = Digest::MD5.new
        @range.step(1024) do |idx|
          digest.update(@data[idx...idx+1024])
        end
      end
    end
  end
end
Other Concurrency
   Approaches
java.util.concurrent
require 'java'
java_import java.util.concurrent.Executors

@count = java.util.concurrent.atomic.AtomicInteger.new

def send_email(executor)
  executor.submit do
    puts "email #{@count.incrementAndGet} sent"
  end
end
executor = Executors.newCachedThreadPool
send_email(executor)



executor =
Executors.newFixedThreadPool(2)
loop do
  send_email(executor)
end
jetlang/jretlang
 http://code.google.com/p/jetlang/
https://github.com/reevesg/jretlang
require 'jretlang'

fiber = JRL::Fiber.new
channel = JRL::Channel.new
fiber.start

channel.subscribe_on_fiber(fiber) do |msg|
  puts msg
end

channel.publish "Hello"
Akka
     http://akka.io
http://j.mp/jruby-akka
require 'akka'

class HelloWord
  def hi
    puts "hello actor world"
  end
end

Actors.actorOf(HelloWord.new).hi
puts "initiating shutdown..."
Actors.delayedShutdown 1

More Related Content

PDF
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
PDF
JRuby @ Boulder Ruby
PPT
JS everywhere 2011
PDF
ECMAScript 6
PDF
Connecting the Worlds of Java and Ruby with JRuby
PDF
JavaScript - new features in ECMAScript 6
PDF
node ffi
PDF
Introduction to reactive programming & ReactiveCocoa
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby @ Boulder Ruby
JS everywhere 2011
ECMAScript 6
Connecting the Worlds of Java and Ruby with JRuby
JavaScript - new features in ECMAScript 6
node ffi
Introduction to reactive programming & ReactiveCocoa

What's hot (20)

PPT
Node js presentation
PDF
ES2015 (ES6) Overview
ODP
Clojure: Practical functional approach on JVM
PDF
Comet with node.js and V8
PDF
Introduction to clojure
PDF
EcmaScript 6 - The future is here
PDF
FalsyValues. Dmitry Soshnikov - ECMAScript 6
KEY
A million connections and beyond - Node.js at scale
PPTX
iSoligorsk #3 2013
ODP
Supercharging reflective libraries with InvokeDynamic
PPTX
Node.js System: The Approach
PDF
Introduction to CoffeeScript
KEY
Introduction to node.js
PDF
Advanced realm in swift
PDF
Streams in Node.js
PDF
Java libraries you can't afford to miss
PPTX
Avoiding Callback Hell with Async.js
PPT
Server side JavaScript: going all the way
PDF
ORMs in Golang
PPTX
introduction to node.js
Node js presentation
ES2015 (ES6) Overview
Clojure: Practical functional approach on JVM
Comet with node.js and V8
Introduction to clojure
EcmaScript 6 - The future is here
FalsyValues. Dmitry Soshnikov - ECMAScript 6
A million connections and beyond - Node.js at scale
iSoligorsk #3 2013
Supercharging reflective libraries with InvokeDynamic
Node.js System: The Approach
Introduction to CoffeeScript
Introduction to node.js
Advanced realm in swift
Streams in Node.js
Java libraries you can't afford to miss
Avoiding Callback Hell with Async.js
Server side JavaScript: going all the way
ORMs in Golang
introduction to node.js
Ad

Similar to Nick Sieger JRuby Concurrency EMRubyConf 2011 (20)

PDF
Bringing Concurrency to Ruby - RubyConf India 2014
KEY
Concurrent programming with Celluloid (MWRC 2012)
KEY
Ruby Concurrency and EventMachine
PDF
Ruby thread safety first
ODP
Concurrent Programming with Ruby and Tuple Spaces
PDF
The Joy Of Ruby
PDF
Dataflow: Declarative concurrency in Ruby
ODP
RailswayCon 2010 - Dynamic Language VMs
KEY
PDF
Quick Intro To JRuby
KEY
jRuby: The best of both worlds
PDF
RubyConf Brazil 2010
PDF
Ruby 程式語言入門導覽
PDF
Ruby Performance - The Last Mile - RubyConf India 2016
KEY
A tour on ruby and friends
PDF
Concurrency: Rubies, Plural
PDF
Concurrency: Rubies, plural
PDF
Story for a Ruby on Rails Single Engineer
PDF
Intro to J Ruby
PDF
Rapid Development with Ruby/JRuby and Rails
Bringing Concurrency to Ruby - RubyConf India 2014
Concurrent programming with Celluloid (MWRC 2012)
Ruby Concurrency and EventMachine
Ruby thread safety first
Concurrent Programming with Ruby and Tuple Spaces
The Joy Of Ruby
Dataflow: Declarative concurrency in Ruby
RailswayCon 2010 - Dynamic Language VMs
Quick Intro To JRuby
jRuby: The best of both worlds
RubyConf Brazil 2010
Ruby 程式語言入門導覽
Ruby Performance - The Last Mile - RubyConf India 2016
A tour on ruby and friends
Concurrency: Rubies, Plural
Concurrency: Rubies, plural
Story for a Ruby on Rails Single Engineer
Intro to J Ruby
Rapid Development with Ruby/JRuby and Rails
Ad

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf
Encapsulation_ Review paper, used for researhc scholars
Advanced methodologies resolving dimensionality complications for autism neur...
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Nick Sieger JRuby Concurrency EMRubyConf 2011