【问题标题】:Can iactorref.tell be called outside from actor from multithread可以从多线程的actor外部调用iactorref.tell吗
【发布时间】:2019-07-19 01:57:47
【问题描述】:

可以多线程调用akka.net中的IactorRef.tell吗? 当然传递给tell方法的消息是不可变的

ActorRef.Tell 线程是否安全?

例子:(下面的代码对吗?)

 class Program
    {
        static void Main(string[] args)
        {
            Props props = Props.Create<PrintMyActorRefActor>();
            var sys = ActorSystem.Create("Sys");
            var actorRef = sys.ActorOf(props, "worker");

            for (int i = 0; i < 21; i++)
            {
//the message passed into tell method will be immutable
                int j = i;
                Task.Factory.StartNew(() =>
                {
                    actorRef.Tell("printit" + j, ActorRefs.NoSender);
                });
            }
            Console.ReadLine();

        }
    }


    public class PrintMyActorRefActor : UntypedActor
    {
        protected override void OnReceive(object message)
        {
            string msgStr = message == null ? "" : message.ToString();

            Console.WriteLine(msgStr);
        }
    }

【问题讨论】:

    标签: c# akka.net


    【解决方案1】:

    是的,Tell 消息是线程安全的,并且在两个参与者之间保持顺序 - 所以参与者 A Tell 向参与者 B 发送一些消息可以指望它们按发送顺序交付 - 但不是跨多个参与者。

    消息必须是不可变的,或者至少作为用户的您必须保证消息的可变组件不会被其他地方访问(因此一旦发送,接收参与者就是该数据的所有者)。

    另一件特定于您的代码的事情是您不需要将actor.TellTask 包装起来:asynchronous message passing 是参与者的基本概念之一,因此发送消息不会阻塞执行线程。

    【讨论】:

    • “另一件特定于您的代码的事情是您不需要包装 actor.Tell 和 Task”:我认为多线程示例应该有助于澄清问题。从不同线程同时使用相同的 ActorRef 实例来告诉消息是否安全?一种小型模拟。
    • 是的,ActorRef 是线程安全的,您可以从任意数量的线程(或远程场景中的机器)向它发送消息。这些消息在内部排队,并由参与者一一处理。请记住,消息的顺序仅在同一个发送者线程的范围内得到保证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2013-03-09
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多