Created
June 24, 2013 04:56
-
-
Save rocky-jaiswal/5847814 to your computer and use it in GitHub Desktop.
Producer Consumer with Actors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'celluloid/autostart' | |
class Cook | |
include Celluloid | |
def produce(manager) | |
manager.async.inform("produced") | |
end | |
end | |
class Consumer | |
include Celluloid | |
def consume(manager) | |
manager.async.inform("consumed") | |
end | |
end | |
class Manager | |
include Celluloid | |
def initialize | |
@items = [] | |
@producer = Cook.new | |
@consumer = Consumer.new | |
end | |
def work | |
@producer.async.produce(Actor.current) | |
end | |
def inform(message) | |
if message == "produced" | |
@items.push message | |
puts Thread.current.to_s + " Produced item .... Stock : " + @items.size.to_s | |
@consumer.async.consume(Actor.current) | |
end | |
if message == "consumed" | |
@items.pop | |
puts Thread.current.to_s + " Consumed item .... Stock : " + @items.size.to_s | |
work | |
end | |
end | |
end | |
manager = Manager.new | |
manager.async.work | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment