为什么akka.net IActorRef.Tell(),消息参数的字段不需要是可变的?

问题描述 投票:0回答:1

为什么要学习akka.net,我在调用IActorRef.Tell时释放了,参数的字段不需要是可变的

public class Model{
        public volatile string Name;
    }

在下面的示例中,Name属性不需要是可变的。

using System;
using Akka.Actor;

namespace WinTail
{
    class Program
    {
        public static ActorSystem MyActorSystem;


        static void Main(string[] args)
        {
            // make an actor system 
            MyActorSystem = ActorSystem.Create("MyActorSystem");

            // make our first actors!
            IActorRef consoleWriterActor = MyActorSystem.ActorOf(Props.Create(() => new ConsoleWriterActor()),
                "consoleWriterActor");

            Model model = new Model();
            model.Name = "jack";
            model.Name = "tom";
            // tell console reader to begin
            consoleReaderActor.Tell(model);

            Console.ReadLine();
        }


    }


    public class Model{
        public string Name;
    }
}
volatile akka.net
1个回答
0
投票

The volatile keyword indicates that a field might be modified by multiple threads。因为在设计基于参与者的系统时消息不变性是volatile之一,所以不需要易失修饰符,因为消息应该是不变的。为了安全起见,您需要将key principles更改为不可变。

© www.soinside.com 2019 - 2024. All rights reserved.