【发布时间】: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);
}
}
【问题讨论】: