public interface ActorThread
Modifier and Type | Method and Description |
---|---|
<T> ActorRef<T> |
bindActor(Class<T> type,
T rawActor)
Binds an actor to this
ActorThread and returns an ActorRef for sending messages to it. |
void |
stop()
Stops all actors which are bound to this
ActorThread after all previously sent messages to them
have been processed. |
<T> ActorRef<T> bindActor(Class<T> type, T rawActor)
ActorThread
and returns an ActorRef
for sending messages to it. The
recommended pattern for creating actors is ActorRef<TheActorInterface> theActor =
actorThread.bindActor(TheActorInterface.class, new TheActor(theParameters));
all in one line.
Extra care should be taken to never pass around the raw actor instance. It should always be used through an
ActorRef
. Use the convention that when passing an actor as a method/constructor parameter to another
class, the type of the parameter is ActorRef<TheActorInterface>
.
All actors bound to the same ActorThread
will be executed in the same Thread
, so it is OK for
them to share some mutable state when it is known that all the actors are bound to the same thread. A common
pattern is to pass an actor its own ActorThread
, so that it can create short-lived actors for callbacks,
or a reference to itself, when communicating with other actors.
After nobody is holding the actor's ActorRef
, that actor will be garbage collected. It is OK to create
lots of short-lived actors.void stop()
ActorThread
after all previously sent messages to them
have been processed. It is not possible to stop just one actor from an ActorThread
, though due to garbage
collection that is usually not needed.
An alternative way to stop actors is to call Thread.interrupt()
on the thread where the actor is running,
which will stop that ActorThread
immediately.Copyright © 2011-2012. All Rights Reserved.