nick sieger jruby concurrency emrubyconf 2011

20
JRuby Concurrency Nick Sieger EMRubyConf @ RailsConf 2011

Upload: nick-sieger

Post on 09-May-2015

1.382 views

Category:

Technology


3 download

DESCRIPTION

Presented at EMRubyConf 2011 alongside RailsConf/BohConf.

TRANSCRIPT

Page 1: Nick Sieger JRuby Concurrency EMRubyConf 2011

JRuby ConcurrencyNick SiegerEMRubyConf @ RailsConf 2011

Page 2: Nick Sieger JRuby Concurrency EMRubyConf 2011

MentalModel

http://www.flickr.com/photos/solar_decathlon/4524503892/

Page 4: Nick Sieger JRuby Concurrency EMRubyConf 2011

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?!

Page 5: Nick Sieger JRuby Concurrency EMRubyConf 2011

How could this code fail?

class ExpensiveToCreate def self.instance @instance ||= ExpensiveToCreate.new endend

Page 6: Nick Sieger JRuby Concurrency EMRubyConf 2011

How could this code fail?

class ExpensiveToCreate def self.instance unless defined?(@instance) @instance = ExpensiveToCreate.new end endend

Page 7: Nick Sieger JRuby Concurrency EMRubyConf 2011

class ExpensiveToCreate def self.instance unless defined?(@instance) @instance = ExpensiveToCreate.allocate @instance.initialize # big pause here @instance end endend

How could this code fail?

T1T2

Page 8: Nick Sieger JRuby Concurrency EMRubyConf 2011

Avoid shared mutable state,lazy initialization

MutableConstant.merge! :key => value

class MemoryCache def self.cache @cache ||= {} endend

$global_lock.do_something!

Page 9: Nick Sieger JRuby Concurrency EMRubyConf 2011

Difficult to observewith green threads

Page 10: Nick Sieger JRuby Concurrency EMRubyConf 2011

data = [] M.times do |m| Thread.new do N.times do |n| data << m * n end end end

Page 11: Nick Sieger JRuby Concurrency EMRubyConf 2011

OK NOT OK

method defclass def

class vars

instance varsStringArrayHash

Page 12: Nick Sieger JRuby Concurrency EMRubyConf 2011

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 endend

Page 13: Nick Sieger JRuby Concurrency EMRubyConf 2011

Other Concurrency Approaches

Page 14: Nick Sieger JRuby Concurrency EMRubyConf 2011

java.util.concurrent

Page 15: Nick Sieger JRuby Concurrency EMRubyConf 2011

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" endend

Page 16: Nick Sieger JRuby Concurrency EMRubyConf 2011

executor = Executors.newCachedThreadPoolsend_email(executor)

executor = Executors.newFixedThreadPool(2)loop do send_email(executor)end

Page 17: Nick Sieger JRuby Concurrency EMRubyConf 2011

jetlang/jretlanghttp://code.google.com/p/jetlang/

https://github.com/reevesg/jretlang

Page 18: Nick Sieger JRuby Concurrency EMRubyConf 2011

require 'jretlang'

fiber = JRL::Fiber.newchannel = JRL::Channel.newfiber.start

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

channel.publish "Hello"

Page 19: Nick Sieger JRuby Concurrency EMRubyConf 2011

Akkahttp://akka.io

http://j.mp/jruby-akka

Page 20: Nick Sieger JRuby Concurrency EMRubyConf 2011

require 'akka'

class HelloWord def hi puts "hello actor world" endend

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