2016-02-15 35 views
0

下面的代码是在叫我的程序的尽头(这是写在JRuby中):阿卡system.shutdown和awaitTermination

@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是否等待所有演员终止?

是不是@@system关闭之前awaitTermination被称为?

编辑:我注意到我甚至不需要拨打tell(PoisonPill)。我评论它,它仍然有效...

回答

0

好吧,我现在解决了它。当我致电system.shutdown时,所有演员在当前任务结束后终止。这不是我想要的,因为队列中可能有更多任务。

因此,我发送一个PoisonPill给每个演员在我的主线程结束,然后等待他们终止。我还在每个演员中使用函数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