How to wait for Akka actor system to terminate?

2020-05-20 08:07发布

I need to start Akka (2.0) actor system, send in some messages, then wait for it to do heavy lifting. After that, I need to do something unrelated to those actors.

I tried to wait for all actors to stop with following code:

val system = new ActorSystem("parallelRunners")
val master = system.actorOf(Props[Master])
master ! Start
system.awaitTermination // <-- hangs here

All actors kill themselves via self ! PoisonPill. What am I doing wrong?

标签: scala akka
7条回答
男人必须洒脱
2楼-- · 2020-05-20 08:32

You can also kill the ActorSystem from the main thread by using system.shutdown. But that operation is asynchronous. You only need to use system.awaitTermination if you need to block the main thread until it completes. If your parallelRunners return something useful for the rest of the program, then it would probably be easiest to not block and continue your program.

查看更多
登录 后发表回答