Java Akka的ActorRef异步问题 [英] Java Akka's ActorRef async issues

查看:219
本文介绍了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异步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆