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…
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
actorOfmethod. 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.
and in your client code