Java IPC 最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice for Java IPC
提问by Stu Thompson
what is the best method for inter process communication in a multithreaded java app.
在多线程 java 应用程序中进行进程间通信的最佳方法是什么。
It should be performant (so no JMS please) easy to implement and reliable,so that objects & data can be bound to one thread only?
它应该是高性能的(所以请不要使用 JMS)易于实现且可靠,以便对象和数据只能绑定到一个线程?
Any ideas welcome!
欢迎任何想法!
采纳答案by Boris Terzic
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 Jetlanglooks very interesting, lightweight Java message passing.
假设场景 1 JVM,那么确实需要查看多线程 java.util.concurrent,特别是各种 Queue 实现。然而,在此之上的抽象可能很好,Jetlang看起来非常有趣,轻量级的 Java 消息传递。
回答by Stu Thompson
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.)
你能澄清一下吗?你是说单个JVM中的IPC吗?(多线程,是的,但在操作系统级别只有一个进程。)还是您的意思是多个 JVM?(和真正的操作系统级进程间通信。)
If it is the first, then maybe something out of java.util.concurrent, like ConcurrentLinkedQueuewould do the trick. (I pass message around inbetween my threads with classes from java.util.concurrent with success.)
如果它是第一个,那么可能来自java.util.concurrent 的东西,比如ConcurrentLinkedQueue可以解决问题。(我使用 java.util.concurrent 中的类成功地在我的线程之间传递消息。)
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.
如果是后者,那么我将只是猜测并建议看一下RMI,尽管我认为它完全可靠 - 您必须更多地“动手”来管理它。
回答by TraderJoeChicago
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.
您应该使用生产者/消费者队列。通过这样做,您可以避免多线程编程的陷阱:竞争条件和死锁。此外,如果您使用 Disruptor 或 MentaQueue 等无锁队列,它不仅更容易和更干净,而且速度也会更快。我写了一篇博客文章,详细讨论了这一点,并展示了如何获得 < 100 纳秒延迟:具有 2 位纳秒延迟的线程间通信。
回答by MikaelJ
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.
我刚刚在 github ( http://github.com/caplogic/mappedbus)上添加了 MappedBus,这是一个高效的 IPC 库,它使多个 Java 进程/JVM 能够通过交换消息进行通信,并且它使用内存映射文件进行传输。吞吐量已测量为 4000 万条消息/秒。
回答by Staale
I recommend looking into the entire java.util.concurrentpackage, 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.
我建议查看整个java.util.concurrent包,它有多个类用于处理并发和线程之间不同的通信方式。一切都取决于您想要实现的目标,因为您的问题非常普遍。

