Nick Sieger JRuby Concurrency EMRubyConf 2011

  • 1,333 views
Uploaded on

Presented at EMRubyConf 2011 alongside RailsConf/BohConf.

Presented at EMRubyConf 2011 alongside RailsConf/BohConf.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,333
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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