Java 从Akka的非参与者发送消息
如果我打电话Java 从Akka的非参与者发送消息,java,akka,Java,Akka,如果我打电话 actorRef.tell("MSG", null); 非演员 它仍然是线程安全的吗?和中的异步一样立即完成 这个消息是不可变的 基本上我的代码是: class NonActor { .... public void tellTest() { actorRef.tell("MSG", null); } } 它是否线程安全取决于应用程序的其余部分。演员并非天生线程安全;您可以像与任何应用程序实体一样共享可变状态。但是tell调用将立即
actorRef.tell("MSG", null);
非演员
它仍然是线程安全的吗?和中的异步一样立即完成
这个消息是不可变的
基本上我的代码是:
class NonActor {
....
public void tellTest() {
actorRef.tell("MSG", null);
}
}
它是否线程安全取决于应用程序的其余部分。演员并非天生线程安全;您可以像与任何应用程序实体一样共享可变状态。但是
tell
调用将立即返回,因为它是异步的。基本上actorRef.tell(“MSG”,null)代码>创建一个类似
(actorRef, Envelope(msg, sender))
并将其放入ActorSystem
的消息队列中。因此,tell
与参与者没有任何联系。tell
方法本身无疑是线程安全的。作为发送者的null意味着接收参与者无法执行sender()。tell()来回答您的消息。只需使用代码>操作员
在Akka classic 2.6中定义如下:
trait ScalaActorRef { ref: ActorRef =>
/**
* Sends a one-way asynchronous message. E.g. fire-and-forget semantics.
* <p/>
*
* If invoked from within an actor then the actor reference is implicitly passed on as the implicit 'sender' argument.
* <p/>
*
* This actor 'sender' reference is then available in the receiving actor in the 'sender()' member variable,
* if invoked from within an Actor. If not then no sender is available.
* <pre>
* actor ! message
* </pre>
* <p/>
*/
def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit
}
trait ScalaActorRef{ref:ActorRef=>
/**
*发送单向异步消息。例如,激发并忘记语义。
*
*
*如果从actor中调用,那么actor引用将作为隐式“sender”参数隐式传递。
*
*
*然后,在“sender()”成员变量中的接收参与者中可以使用此参与者“sender”引用,
*如果从参与者内部调用,则没有可用的发送者。
*
*演员!留言
*
*
*/
def!(消息:Any)(隐式发送方:ActorRef=Actor.noSender):单位
}
actorRef.tell(“MSG”,null)
就像actorRef。!(“MSG”)(null)
,强制发送方为null
如果您使用
在Actor外部,将找不到隐式发送方,因此编译器将放入默认值Actor.noSender
只要发送方的缺失应该在编译时自动解决,那么显式地告诉Akka它是null
可能容易出现错误
永远不要使用null
是Scala 你是说取决于味精本身吗?只要它是不可变的?只要msg是不可变的,并且参与者不使用任何共享的可变状态。这些都是我对它工作的想法,事实上也是如此。在文件中找到并在几天前确认。