繁体   English   中英

我如何在不使用Thread.sleep命令的情况下在akka actor中执行整个代码?

[英]how can i execute whole code in akka actor without using the Thread.sleep command?

我有一个示例akka代码

public class MainSystem {
  public static void main(String... args) throws Exception {
        final ActorSystem actorSystem = ActorSystem.create("actor-system");
        Thread.sleep(5000);
        final ActorRef actorRef = actorSystem.actorOf(SimpleActor.props(10));
        final ActorRef actorRef2 = actorSystem.actorOf(ActorTwo.props(10));
        System.out.println("actorref2:  "+actorRef2);

        actorRef2.tell(new Command("actor two cmd"), null);

        actorRef.tell(new Command("CMD 1"), null);
        actorRef.tell(new Command("CMD 2"), null);

        actorRef2.tell(new Command("actor two cmd   second"), null);


        actorRef.tell(new Command("CMD 3"), null);
        actorRef.tell(new Command("CMD 4"), null);
        actorRef.tell(new Command("CMD 5"), null);

        Thread.sleep(5000);

        actorSystem.shutdown();
    }
}

如果我避免使用最后一个sleep语句,则代码将无法完全执行。 如何在不使用睡眠的情况下编写程序以完成actor中的所有代码?

您可以在收到并处理了最后一条消息后从actor内部关闭系统,然后使用actorSystem.awaitTermination()等待终止。

编辑

Akka中的Shut Down Patterns是一个不错的博客,介绍了shutdown模式。 您现在可能不需要太多,但是您将获得要点。 该代码在scala中,但并不那么复杂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM