Reputation:
In Java I have a hobby with thread, for example:
Thread thread = new Thread() {
@Override
public void run() {
//use this while loop, I can stop/ interrupt the thread when I want
while (!isInterrupted()) {
//...
}
}
};
thread.start();
and then, when I want to stop/ interrupt my thread:
thread.interrupt();
So my question is: is there a built-in way (a field/ method/ function...) in Scala that I can stop/ interrupt an actor?
This is my background: I'm new to Scala, I'm really learning to code. And I'm finding my best way between actor and thread. From my point of view, I like the new approach - actor.
P.S: Sorry for my English...
Upvotes: 2
Views: 1666
Reputation: 1912
As indicated by the other answer, the actor model is specifically designed to restrict all interaction with an Actor to message passing and handling. Scala/Akka accomplish this by forcing you to create an Actor by calling the actorOf
method. Since you don't have a reference to the underlying object, you can't call methods on it directly.
So, if you want to have an Actor that can be interrupted, just handle some message that does that.
def receive = {
case 'Interrupt => // handle interruption...
}
and in your client code
a ! 'Interrupt
Upvotes: 1
Reputation: 2399
You can send an Actor a PoisonPill to ask him to terminates (see http://akka.io/docs/akka/1.2/intro/getting-started-first-scala.html)
Please note that that works with akka Actors. Don't know for scala actors.
Upvotes: 0