2016-02-15 43 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