14

what is the best method for inter process communication in a multithreaded java app.

It should be performant (so no JMS please) easy to implement and reliable,so that objects & data can be bound to one thread only?

Any ideas welcome!

Andrei Petrenko
3,9483 gold badges34 silver badges54 bronze badges
asked Sep 9, 2008 at 9:20
2
  • 2
    IPC is "Inter-process communication". Commented May 4, 2012 at 20:41
  • IPC usually means across different JVMs. This is usually done through shared memory and memory-mapped files. Check the open-source CoralRing project for the full details. This is not trivial to get it right as you need to step out of the JVM through sun.misc.Unsafe. Commented Nov 22, 2024 at 1:46

5 Answers 5

14

Could you clarify a bit? Do you mean IPC in a single JVM? (Multiple threads, yes, but at an OS-level only one process.) Or do you mean multiple JVMs? (And truly OS-level inter process communications.)

If it is the first, then maybe something out of java.util.concurrent, like ConcurrentLinkedQueue would do the trick. (I pass message around inbetween my threads with classes from java.util.concurrent with success.)

If the later, then I'm going to just guess and suggest taking a look at RMI , although I don' think it qualifies as fully reliable--you'd have to manage that a bit more 'hands on' like.

answered Sep 9, 2008 at 9:43
Sign up to request clarification or add additional context in comments.

4 Comments

RMI is by far the best way for IPC between different JVMs.
That's not always true. RMI might be simple, but it does require that the interface from which stubs are generated remain static, without changes. The choice of IPC depends on the needs of the application. A message passing IPC mechanism might be better in some use cases.
IPC is "Inter-process communication", The classes in java.util.concurrent are designed for several threads of a single process, not different processes.
@Hugo: Thanks for the -1. Don't get so hung up on labeling things threads or processes. Once you abstract it all a bit, threads and processes are the same logically; the same issues must be dealt with. In the end it is just an implementation question. Hence the dual nature of my answer.
9

Assuming the scenario 1 JVM, multiple threads then indeed java.util.concurrent is the place to look, specifically the various Queue implementations. However an abstraction on top of that may be nice and there Jetlang looks very interesting, lightweight Java message passing.

answered Sep 9, 2008 at 10:29

Comments

4

I've just added MappedBus on github (http://github.com/caplogic/mappedbus) which is an efficient IPC library that enable several Java processes/JVMs to communicate by exchaning messages and it uses a memory mapped file for the transport. The troughput has been measured to 40 million messages/s.

answered May 15, 2015 at 8:42

Comments

3

I recommend looking into the entire java.util.concurrent package, which have multiple classes for dealing with concurrency and different communication means between threads. All depends on what you want to achieve, as your question is pretty general.

answered Sep 9, 2008 at 9:54

Comments

3

You should use a producer/consumer queue. By doing that you avoid the pitfalls of multithreaded programming: race-conditions and deadlocks. Plus it is not just easier and cleaner, but also much faster if you use a lock-free queue like Disruptor or MentaQueue. I wrote a blog article where I talk about this in detail and show how to get < 100 nanoseconds latencies: Inter-thread communication with 2-digit nanosecond latency.

answered Nov 27, 2012 at 2:16

1 Comment

your link require basic auth.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.