Java Akka的ActorRef异步问题

编程入门 行业动态 更新时间:2024-10-10 19:18:06
本文介绍了Java Akka的ActorRef异步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经开始使用Akka与并发程序进行异步:

I have started to use Akka to do async with concurrent program:

import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.actor.UntypedActor; public class TestAkka { public static void main(String[] args) throws InterruptedException { ActorSystem as1 = ActorSystem.create("actor1"); ActorRef ar1 = as1.actorOf(Props.create(Hello.class)); System.out.println("Start to say hello!"); ar1.tell("Bob", ActorRef.noSender()); ar1.tell("John", ActorRef.noSender()); System.out.println("Finish to say hello!"); } public static class Hello extends UntypedActor { @Override public void onReceive(Object message) throws Exception { if (message instanceof String) { System.out.println("Hello " + message); Thread.sleep(10000); // <--Sim the job take a short time } } } }

我执行上述程序,系统必须一个接一个地完成(非并发):

I execute above program, the system must finish one by one (not concurrent):

ar1.tell("Bob", ActorRef.noSender()); ar1.tell("John", ActorRef.noSender());

所以结果是:

Hello Bob (Wait 5 seconds) Hello John (Wait 5 seconds)

我想让它们并发,如何处理?我希望Akka能够自动处理它:)谢谢您的想法!

I want to make them concurrent, how to handle it? I expect Akka should auto handle it:) Thanks for your idea!

推荐答案

Akka的原理(以及一般的参与者模型) )是指在单个演员中,所有操作都是顺序发生的。这具有几个优点,包括在处理自己的可变状态时,actor可以是无锁的。并发是通过让多个参与者同时运行来实现的。

The principle of Akka (and the actor model in general) is that within a single actor, everything happens sequentially. This has several advantages including that an actor can be lock-free when handling its own mutable state. Concurrency is achieved through having multiple actors running simultaneously.

因此,如果您创建两个 Hello 参与者,并分别向他们发送消息,他们将同时处理它们。 (假设您的akka​​执行上下文具有足够的线程)。

Therefore if you create two Hello actors and send them each a message, they will process them concurrently. (assuming your akka execution context has enough Threads).

import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; import akka.actor.UntypedActor; public class TestAkka { public static void main(String[] args) throws InterruptedException { ActorSystem as1 = ActorSystem.create("actor1"); ActorRef ar1 = as1.actorOf(Props.create(Hello.class)); ActorRef ar2 = as1.actorOf(Props.create(Hello.class)); System.out.println("Start to say hello!"); ar1.tell("Bob", ActorRef.noSender()); ar2.tell("John", ActorRef.noSender()); System.out.println("Finish to say hello!"); } public static class Hello extends UntypedActor { @Override public void onReceive(Object message) throws Exception { if (message instanceof String) { System.out.println("Hello " + message); Thread.sleep(10000); // <--Sim the job take a short time } } } }

更多推荐

Java Akka的ActorRef异步问题

本文发布于:2023-07-15 13:15:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Akka   ActorRef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!