Erlang JInterface - is OtpMBox thread-safe?

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?

Asked by: Wilson456 | Posted: 23-01-2022

Answer 1

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.

Answered by: Dainton460 | Posted: 24-02-2022

Answer 2

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.

Answered by: Julian117 | Posted: 24-02-2022

Answer 3

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

Answered by: Arthur765 | Posted: 24-02-2022

Answer 4

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.

Answered by: Sam547 | Posted: 24-02-2022

Similar questions

Are arrays thread-safe in Java?

Are there any concurrency problems with one thread reading from one index of an array, while another thread writes to another index of the array, as long as the indices are different? e.g. (this example not necessarily recommended for real use, only to illustrate my point) class Test1 { static final private int N = 4096; final private int[] x = new int[N]; final private AtomicIntege...

Thread-safe setting of a variable (Java)?

Given the following code: public class FooBar { public static volatile ConcurrentHashMap myConfigData = new ConcurrentHashMap(); } public class UpdaterThread implements Runnable { public void run() { //Query the Data from the DB and Update the FooBar config Data FooBar.myConfigData = ConfigDataDAO.getLatestConfigFromDB(); } } The Thread-Class will update the myC...

java - Not thread-safe Object publishing

Reading "Java Concurrency In Practice", there's this part in section 3.5: public Holder holder; public void initialize() { holder = new Holder(42); } Besides the obvious thread safety hazard of creating two instances of Holder, the book claims a possible publishing issue can occur. Furthermore, for a Holder class such as public Holder ...

java - Is the following utility class thread-safe?

First let's look at the utility class (most javadoc has been removed to simply the example): public class ApplicationContextUtils { /** * The application context; care should be taken to ensure that 1) this * variable is assigned exactly once (in the * {@link #setContext(ApplicationContext)} method, 2) the context is never * reassigned to {@code null}, 3) access to the field is thre...

Is Java's TimeZone thread-safe?

I wanted my application to just have one TimeZone object which will be used by many SimpleDateFormat and Calendar objects from the other places concurrently. This is to avoid having to always do TimeZone.getTimeZone(ID). I know SimpleDateFormat and Calendar classes are not thread safe, which is why I configure one thread to always crea...

java - If all collection attributes are thread-safe , can we say that this collection is thread-safe?

If all attributes (or items fields, or data members) of a java collection are thread-safe (CopyOnWriteArraySet,ConcurrentHashMap, BlockingQueue, ...), can we say that this collection is thread-safe ? an exemple : public class AmIThreadSafe { private CopyOnWriteArraySet thradeSafeAttribute; public void add(Object o) { ...

How thread-safe is enum in java?

How thread-safe is enum in java? I am implementing a Singleton using enum (as per Bloch's Effective Java), should I worry at all about thread safety for my singleton enum? Is there a way to prove or disprove that it is thread safe? // Enum singleton - the preferred approach public enum Elvis { INSTANCE; public void leaveTheBuilding() { ... } } Thanks

java - Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively?

I have a linked list samples: protected LinkedList<RawDataset> samples = new LinkedList<RawDataset>(); I'm appending elements to the list in thread 1 like this: this.samples.offer(data); And I'm retrieving elements from it in a second thread like so: public RawDataset retrieveSample() { return this.samples.poll(...

caching - Thread-safe cache of one object in java

let's say we have a CountryList object in our application that should return the list of countries. The loading of countries is a heavy operation, so the list should be cached. Additional requirements: CountryList should be thread-safe CountryList should load lazy (only on demand) CountryList should support the invalidation of the cache CountryList should be optimized consi...

java - Thread-safe HashSet with Guava Collections

Like the title says, i would like to get a thread-safe HashSet using Guava Collections. Are any available?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)