Reputation: 3783
What is the best way to shut down an object which is itself running a Thread from within an Actor. I'm not sure what the best strategy is?
Most topics seem to revolve around the idea of using Threads for everything, but I'm doing Blocking IO and it's not an option to rewrite 3rd party code ;)
Is there anything particularly import to be aware of?
Upvotes: 0
Views: 85
Reputation: 134270
It's not clear on exactly what you mean, so apologies if I have the wrong end of the stick. You have some Actor
class A
, which has as part of its state an object which is some 3rd-party API which causes a separate thread to run (and maybe to issue callbacks to it?)
class A extends Actor {
val api = { ... }
def act() = { ... }
}
You wish to shut down an instance of your actor, A
and you are concerned about the thread created by api
and what will happen to it?
If your third-party API provides some mechanism to cleanly dispose of api
(e.g. a destroy
method), then simply call that! Or send an explicit kill signal to the actor and have it exit gracefully:
case 'Kill => api.destroy(); self.exit()
If the API provides no such mechanism, then you are a bit stuck (and it is a terrible API, I might add). You can shut your actor down, but the separate thread will remain running. Of course, it may be a daemon thread, and hence not prevent a graceful shutdown of the JVM as a whole. Otherwise, the only way of gracefully exiting the program is to explicitly call sys.exit(0)
from some appropriate place.
Upvotes: 3