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



为什么要学习 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关键字指示一个字段可能被多个线程修改。由于消息不变性是设计基于参与者的系统时的关键原则之一,因此不需要易失性修饰符,因为消息应该是不可变的。为了安全起见,您需要将Model更改为不可变。

最新更新