Erlang JInterface - OtpMBox 线程安全吗?
在我的 Java 程序中,我创建了一个 OtpNode 和一个“命名” OtpMBox。 每当通过此 mbox 接收消息时,都需要执行一些耗时的操作,然后发回回复消息。 由于此操作非常耗时,因此发送到 mbox 的后续消息不会立即得到处理。
所以我想使用 Java 线程 - 每收到一条消息就使用一个线程。 我的问题是我还应该创建一个新的 OtpMBox 对于每条收到的消息,或者原始的 OtpMBox 可以在所有线程之间共享吗?
In my Java program, I create an OtpNode and a "named" OtpMBox. Whenever a message is received via this mbox, some time-consuming operation needs to be performed after which a reply message is sent back. Since this operation is time-consuming, subsequent messages sent to the mbox won't be processed immediately.
So I want to use Java threads - one per message received. My question is should I also create a new OtpMBox for each message received or can the original OtpMBox be shared among all the threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以共享 OtpMBox 对象并从多个线程使用它。 这个 关于 jinterface 线程安全的 erlang-questions 线程讨论了这个问题。
另外,对于纯java特定问题,您可能需要使用 来自 java.util.concurrent 的 ThreadPoolExecutor 用于处理到达的消息。
You can share the OtpMBox object and use it from multiple threads. This erlang-questions thread about jinterface threadsafety discusses the matter.
Also, for the pure java specific matters you probably want to use ThreadPoolExecutor from java.util.concurrent for handling the messages that arrives.
我不确定我是否理解这个问题。 您希望每个线程有一个 OtpMBox 才能发送回复,还是这个长时间运行的操作必须能够接收更多消息?
如果是前者,您可以重复使用原始 mbox。 发送操作是同步的。
如果是后者,最好采用 Erlang 方式,为每个线程创建一个 mbox,并让 erlang 端的调用者知道它的 pid,以便它可以将数据发送到该 mbox。 这是因为 jinterface 没有选择性接收,消息将到达首先唤醒的任何线程。
I'm not sure I understand the question. You would want an OtpMBox per thread just to be able to send the reply, or will this long-running operation have to be able to receive further messages?
If the former, you can reuse the original mbox. The send operation is synchronized.
If the latter, it would be better to do it the Erlang way, creating a mbox for each thread and letting the caller from the erlang side know about it's pid so that it can send data to that mbox. This is because jinterface doesn't have selctive receive and the messages will get to whatever thread wakes up first.
我对这个东西不太熟悉,但我想你可以做一些计算)
您有为每个 java 线程运行 OtpMBox 的开销以及控制系统(用 java 编写)的开销,该系统会要求不同的线程做一些工作并从中获取结果。 我相信 java 不是一个好的工具)
你最好做 java-thread 'supervisor',它将用 OtpMBox 启动一些(可能是 CPU 数量)数量的 'worker' java-threads 并将 OtpMBox 的 pid 发送到 erlang 系统。
--对不起我的英语
I'm not really familiar with this stuff, but I suppose you may do some calculates )
You have overhead of running OtpMBox for each java-thread and overhead of controlling system (written in java) that would ask different threads to do some work and take results from them. I believe java isn't good tool for it )
You better do java-thread 'supervisor' that will start some (may be number of CPUs) amount 'worker' java-threads with OtpMBox and send OtpMBox's pids to erlang system.
--sorry my english
听起来你正在尝试使用 java 来做 erlang 擅长的事情。 安全的轻量级多处理。 您是否需要使用 java 进行处理,可以在 erlang 中完成吗? 或者相反,如果 java 无论如何都要执行线程,为什么还要使用 erlang。 我认为也许更多的信息有助于回答这个问题。
It sounds like you are trying to use java to do what erlang is good at. Safe lightweight multiprocessing. Is there a reason why you need to use java for the processing could it be done in erlang instead? Or conversly why are you using the erlang if the java is going to be doing threads anyway. I think perhaps more information would be useful in answering this question.