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