views:

389

answers:

4

In my Java program, I create a 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?

A: 

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

JLarky
A: 

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.

Jeremy Wall
+1  A: 

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.

Vlad Dumitrescu
A: 

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.

Christian
Ok, but how to identify which response comes to what thread?
Rekin