I'm writing my HelloWorld for Akka, I have fixed (20 to 50) number of actors created in a Main Actor, half of them are simple counters, another half calculates factorial and sums results in some way. The main goal was to test how Akka handles naughty hung threads. I added Thread.sleeps with semi-random time to my threads and defined a timeout by scheduling special ForceShutdown case.
What I wanted to get:
What I got: 1 and 2 are working right, but:
I tried several variants of ForceShutdown case, but most of them work just the same, waiting for the child actor to finish unfinished work. Here's an example:
case ForceShutdown =>
println(s"[${self.path.name}] ForceShutdown received!")
children foreach { child => system.stop(child) }
system shutdown
So, how to force terminate hung actors in Akka?
On the JVM there is no way to forcefully shut anything down, since terminating something within the same process will likely corrupt shared state and lead to a broken application. Your idea with the Future will appear to work at first, but the thread pool that runs your runaway code will not shut down, meaning that the JVM will not stop or will accumulate defunct thread pools and eventually run out of memory.
As a consequence you should design all your actors to stay responsive: if you need to run long processing tasks you should break them up into short steps that the actor sends to itself. That way a termination request will be honored in due time.