JRubyConcurrencyNick SiegerEMRubyConf @ RailsConf 2011
MentalModel         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) != RSTR...
How could this code fail?class ExpensiveToCreate  def self.instance    @instance ||= ExpensiveToCreate.new  endend
How could this code fail?class ExpensiveToCreate  def self.instance    unless defined?(@instance)      @instance = Expensi...
How could this code fail?     class ExpensiveToCreate       def self.instanceT2       unless defined?(@instance)          ...
Avoid shared mutable state,     lazy initialization              MutableConstant.merge! :key => valueclass MemoryCache  de...
Difficult to observewith green threads
data = []M.times do |m|  Thread.new do    N.times do |n|      data << m * n    end  endend
OK          NOT OK             instance varsmethod def                String class def                Arrayclass vars     ...
class WorkerTask  def run    @thread = Thread.new do      50.times do        digest = Digest::MD5.new        @range.step(1...
Other Concurrency   Approaches
java.util.concurrent
require javajava_import java.util.concurrent.Executors@count = java.util.concurrent.atomic.AtomicInteger.newdef send_email...
executor = Executors.newCachedThreadPoolsend_email(executor)executor =Executors.newFixedThreadPool(2)loop do  send_email(e...
jetlang/jretlang http://code.google.com/p/jetlang/https://github.com/reevesg/jretlang
require jretlangfiber = JRL::Fiber.newchannel = JRL::Channel.newfiber.startchannel.subscribe_on_fiber(fiber) do |msg|  put...
Akka     http://akka.iohttp://j.mp/jruby-akka
require akkaclass HelloWord  def hi    puts "hello actor world"  endendActors.actorOf(HelloWord.new).hiputs "initiating sh...
Upcoming SlideShare
Loading in...5
×

Nick Sieger JRuby Concurrency EMRubyConf 2011

1,392

Published on

Presented at EMRubyConf 2011 alongside RailsConf/BohConf.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,392
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Nick Sieger JRuby Concurrency EMRubyConf 2011

  1. 1. JRubyConcurrencyNick SiegerEMRubyConf @ RailsConf 2011
  2. 2. MentalModel http://www.flickr.com/photos/solar_decathlon/4524503892/
  3. 3. http://twitter.com/RoflscaleTips/status/61135427813384192
  4. 4. 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?!
  5. 5. How could this code fail?class ExpensiveToCreate def self.instance @instance ||= ExpensiveToCreate.new endend
  6. 6. How could this code fail?class ExpensiveToCreate def self.instance unless defined?(@instance) @instance = ExpensiveToCreate.new end endend
  7. 7. How could this code fail? class ExpensiveToCreate def self.instanceT2 unless defined?(@instance) @instance = ExpensiveToCreate.allocateT1 @instance.initialize # big pause here @instance end end end
  8. 8. Avoid shared mutable state, lazy initialization MutableConstant.merge! :key => valueclass MemoryCache def self.cache @cache ||= {} $global_lock.do_something! endend
  9. 9. Difficult to observewith green threads
  10. 10. data = []M.times do |m| Thread.new do N.times do |n| data << m * n end endend
  11. 11. OK NOT OK instance varsmethod def String class def Arrayclass vars Hash
  12. 12. 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
  13. 13. Other Concurrency Approaches
  14. 14. java.util.concurrent
  15. 15. require javajava_import java.util.concurrent.Executors@count = java.util.concurrent.atomic.AtomicInteger.newdef send_email(executor) executor.submit do puts "email #{@count.incrementAndGet} sent" endend
  16. 16. executor = Executors.newCachedThreadPoolsend_email(executor)executor =Executors.newFixedThreadPool(2)loop do send_email(executor)end
  17. 17. jetlang/jretlang http://code.google.com/p/jetlang/https://github.com/reevesg/jretlang
  18. 18. require jretlangfiber = JRL::Fiber.newchannel = JRL::Channel.newfiber.startchannel.subscribe_on_fiber(fiber) do |msg| puts msgendchannel.publish "Hello"
  19. 19. Akka http://akka.iohttp://j.mp/jruby-akka
  20. 20. require akkaclass HelloWord def hi puts "hello actor world" endendActors.actorOf(HelloWord.new).hiputs "initiating shutdown..."Actors.delayedShutdown 1
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×