Actors are a way to handle concurrent (and parallel) program in a relatively simple way. I give examples in Ruby, but also from other languages like Erlang and Scala.
@chairs = [] puts "lights on ..." end def waiting_line @chairs.size end def new_customer(name) puts " customer arriving: #{name}" if waiting_line > 3 puts "*** sorry, #{name}, no room ***" else @chairs << name @barber.cut_customer!(name) end end def customer_leaves @chairs.shift end end
= self(), BarberPid = spawn(fun() -> barber(ShopPid) end), barber_shop_in_business(BarberPid, []). barber_shop_in_business(BarberPid, CustomersInChairs) -> receive {cut, Customer} -> % pop customer from list when hair is being cut remove_customer(BarberPid, CustomersInChairs, Customer); barbercheck -> respond_to_barber(BarberPid, CustomersInChairs); Customer -> io:format("~p arrives~n",[Customer]), % should check the number of customers waiting first % if all full: customer turned away add_customer_if_available(BarberPid, CustomersInChairs, Customer) end.
{ new } actor.set_dispatcher(Akka::Dispatchers.newThreadBasedDispatcher(actor)) actor.start end def onReceive(msg) case msg when "go" puts 'play score' play_guitar play_drum when "done drumming" play_end_drum when "done guitar" play_guitar else puts "msg: #{msg}" end end def play_end_drum puts "play end drum" end_drum = EndDrum.spawn end_drum.send_one_way("play", context) end (...) end
= Akka::Actors.actor_of { new } actor.set_dispatcher(Akka::Dispatchers.newThreadBasedDispatche r(actor)) actor.start end def onReceive(msg) case msg when "play" @conductor = context.sender.get @conductor = Akka::Actors.registry.actor_for(@conductor.uuid).get play(@conductor) else puts "msg: #{msg}" end end end
Pictures By The Waterfront Vertigo Casablanca Tarzan Wizard of Oz Some like it hot King Kong Citizen Kane Documentation http://www.delicious.com/elisehuard/concurrency