在我的程序的最后调用以下代码(它是用JRuby编写的):
@na.tell(PoisonPill) if defined? @na # @na, @sa and @pe are Actors
@sa.tell(PoisonPill) if defined? @sa
@pe.tell(PoisonPill) if defined? @pe
@@system.shutdown # @@system is the ActorSystem
@@system.awaitTermination
我找到了这种方法here,但我不明白为什么会这样做。
awaitTermination
等待所有Actors终止吗?
在调用@@system
之前,awaitTermination
被关闭了吗?
编辑:我注意到我甚至不需要致电tell(PoisonPill)
。我评论了它,它仍然有用......
答案 0 :(得分:0)
好的,我现在解决了。当我呼叫system.shutdown
时,所有演员都会在当前任务之后终止。这不是我想要的,因为队列中可能有更多的任务。
所以我在主线程结束时向每个actor发送一个PoisonPill,然后等待它们终止。我还在每个actor中使用函数postStop
设置finished
标志,并在所有演员完成后关闭system
。
import Actors # needed for Java-style poisonPill
actor1.tell(Actors::poisonPill) # for Akka 2.0.0
actor2.tell(Actors::poisonPill)
system.awaitTermination