java Java进程间通信和线程间通信?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2527847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:36:49  来源:igfitidea点击:

Java Inter Process communication and Inter Thread communication?

javamultithreading

提问by JavaUser

What is the difference between a Thread and a Process in the Java context? How is inter-Process communication and inter-Thread communication achieved in Java? Please point me at some real life examples.

Java 上下文中的线程和进程有什么区别?Java中如何实现进程间通信和线程间通信?请指点我一些现实生活中的例子。

回答by Stephen C

The fundamental difference is that threads live in the same address spaces, but processes live in the different address spaces. This means that inter-thread communication is about passing references to objects, and changing shared objects, but processes is about passing serialized copies of objects.

根本区别在于线程生活在相同的地址空间中,而进程生活在不同的地址空间中。这意味着线程间通信是关于传递对对象的引用和更改共享对象,而进程是关于传递对象的序列化副本。

In practice, Java interthread communication can be implemented as plain Java method calls on shared object with appropriate synchronization thrown in. Alternatively, you can use the new concurrency classes to hide some of the nitty-gritty (and error prone) synchronization issues.

在实践中,Java 线程间通信可以实现为对共享对象进行普通 Java 方法调用并引入适当的同步。或者,您可以使用新的并发类来隐藏一些细节(和容易出错)的同步问题。

By contrast, Java interprocess communication is based at the lowest level on turning state, requests, etc into sequences of bytes that can be sent as messages or as a stream to another Java process. You can do this work yourself, or you can use a variety of "middleware" technologies of various levels of complexity to abstract away the implementation details. Technologies that may be used include, Java object serialization, XML, JSON, RMI, CORBA, SOAP / "web services", message queing, and so on.

相比之下,Java 进程间通信是基于将状态、请求等转换为字节序列的最低级别,这些字节序列可以作为消息或作为流发送到另一个 Java 进程。您可以自己完成这项工作,也可以使用各种复杂程度不同的“中间件”技术来抽象出实现细节。可能使用的技术包括 Java 对象序列化、XML、JSON、RMI、CORBA、SOAP/“Web 服务”、消息队列等。

At a practical level, interthread communication is many orders of magnitude faster than interprocess communication, and allows you to do many things a lot more simply. But the downside is that everything has to live in the same JVM, so there are potential scalability issues, security issues, robustness issues and so on.

在实际层面上,线程间通信比进程间通信快许多数量级,并且可以让你更简单地做很多事情。但缺点是一切都必须存在于同一个 JVM 中,因此存在潜在的可扩展性问题、安全性问题、健壮性问题等等。

回答by Edwin Buck

A thread can access memory inside a process, even memory that could be manipulated by another thread within the same process. Since all threads are internal to the same running process, they can communicate more quickly (because they don't need the operating system to referee).

线程可以访问进程内的内存,甚至可以由同一进程内的另一个线程操作的内存。由于所有线程都在同一运行进程内部,因此它们可以更快地进行通信(因为它们不需要操作系统进行裁判)。

A process cannot access memory inside another process, although you can communicate between processes through various means like:

一个进程不能访问另一个进程内的内存,尽管您可以通过各种方式在进程之间进行通信,例如:

  1. Network packages.
  2. Files
  3. Pipes
  4. Shared Memory
  5. Semaphores
  6. Corba messages
  7. RPC calls
  1. 网络包。
  2. 文件
  3. 管道
  4. 共享内存
  5. 信号量
  6. Corba 消息
  7. RPC 调用

The important thing to remember with process to process communication is that the communication must be managed through the operating system, and like all things which require a middle man, that adds overhead.

进程间通信需要记住的重要一点是通信必须通过操作系统进行管理,就像所有需要中间人的事情一样,这会增加开销。

On the downside, if a thread misbehaves, it does so within the running process, and odds are high it will be able to take down all the well behaving threads. If a process misbehaves, it can't directly write into the memory of the other processes, and odds are that only the misbehaving process will die.

不利的一面是,如果一个线程行为不端,它会在正在运行的进程中发生,并且很有可能它能够删除所有行为良好的线程。如果一个进程行为不端,它不能直接写入其他进程的内存,而且很可能只有行为不端的进程会死。

回答by rdalmeida

Inter-Thread Communication = threads inside the same JVM talking to each other

线程间通信 = 同一 JVM 中的线程相互通信

Inter-Process Communication (IPC) = threads inside the same machine but running in different JVMs talking to each other

进程间通信 (IPC) = 同一台机器内但运行在不同 JVM 中的线程相互通信

Threads inside the same JVM can use pipelining through lock-free queues to talk to each other with nanosecond latency.

同一 JVM 中的线程可以通过无锁队列使用流水线以纳秒级延迟相互通信。

Threads in different JVMs can use off-heap shared memory (usually acquired through the same memory-mapped file) to talk to each other with nanosecond latency.

不同 JVM 中的线程可以使用堆外共享内存(通常通过相同的内存映射文件获取)以纳秒级延迟相互通信。

Threads in different machines can use the network to talk to each other with microsecond latency.

不同机器中的线程可以使用网络以微秒级的延迟相互通信。

For a complete explanation about lock-free queues and IPC you can check CoralQueue.

有关无锁队列和 IPC 的完整说明,您可以查看CoralQueue

Disclaimer: I am one of the developers of CoralQueue.

免责声明:我是 CoralQueue 的开发人员之一。

回答by Mark M

I like to think of a single instance of a JVM as a process. So, interprocess communication would be between instances of JVM's, for example, through sockets (message passing).

我喜欢将 JVM 的单个实例视为一个进程。因此,进程间通信将在 JVM 的实例之间进行,例如,通过套接字(消息传递)。

Threads in java implement Runnable and are contained within a JVM. They share data simply by passing references in the JVM around. Whenever threads share data you almost always need to protect the data so multiple threads don't clobber each other. There are many mechanisms for protection that all involve preventing multiple threads from entering critical sections of code.

java 中的线程实现 Runnable 并包含在 JVM 中。它们只是通过在 JVM 中传递引用来共享数据。每当线程共享数据时,您几乎总是需要保护数据,以免多个线程相互破坏。有许多保护机制都涉及防止多个线程进入代码的关键部分。