@@ -29,40 +29,46 @@ def on_message(message)
2929counter . ask ( 0 ) . value # => 7
3030
3131# Terminate the actor.
32- counter . tell ( :terminate! ) # => #<Concurrent::Actor::Reference /first (Counter)>
32+ counter . tell :terminate! # => #<Concurrent::Actor::Reference /first (Counter)>
3333# Not terminated yet, it takes a while until the message is processed.
34- counter . terminated? # => false
34+ counter . ask! : terminated? # => true
3535# Waiting for the termination.
36- counter . terminated . class # => Concurrent::Event
37- counter . terminated . wait # => true
38- counter . terminated? # => true
36+ event = counter . ask! ( :terminated_event )
37+ # => #<Concurrent::Event:0x007f6208da3d30 @set=true, @mutex=#<Mutex:0x007f6208da3ce0>, @condition=#<Concurrent::Condition:0x007f6208da3cb8 @condition=#<Thread::ConditionVariable:0x007f6208da3c90>>>
38+ event . class # => Concurrent::Event
39+ event . wait # => true
40+ counter . ask! :terminated? # => true
3941# Any subsequent messages are rejected.
4042counter . ask ( 5 ) . wait . rejected? # => true
4143
4244# Failure on message processing terminates the actor.
4345counter = Counter . spawn ( :first , 0 ) # => #<Concurrent::Actor::Reference /first (Counter)>
4446counter . ask ( 'boom' ) . wait . rejected? # => false
45- counter . terminated? # => false
47+ counter . ask! : terminated? # => false
4648
4749
4850# Lets define an actor creating children actors.
4951class Node < Concurrent ::Actor ::Context
52+ def initialize
53+ @last_child_id = 0
54+ end
55+
5056 def on_message ( message )
5157 case message
5258 when :new_child
53- Node . spawn : child
59+ Node . spawn " child- #{ @last_child_id += 1 } "
5460 when :how_many_children
5561 children . size
5662 else
57- raise 'unknown'
63+ pass
5864 end
5965 end
6066end
6167
6268# Actors are tracking parent-child relationships
6369parent = Node . spawn :parent # => #<Concurrent::Actor::Reference /parent (Node)>
6470child = parent . tell ( :new_child ) . ask! ( :new_child )
65- # => #<Concurrent::Actor::Reference /parent/child (Node)>
71+ # => #<Concurrent::Actor::Reference /parent/child-2 (Node)>
6672child . parent # => #<Concurrent::Actor::Reference /parent (Node)>
6773parent . ask! ( :how_many_children ) # => 2
6874
@@ -72,5 +78,5 @@ def on_message(message)
7278
7379# Termination of an parent will also terminate all children.
7480parent . ask ( 'boom' ) . wait
75- parent . terminated? # => true
76- child . terminated? # => true
81+ counter . ask! : terminated? # => false
82+ counter . ask! : terminated? # => false
0 commit comments