Java Inter Process通信和Inter Thread通信? [英] Java Inter Process communication and Inter Thread communication?

查看:134
本文介绍了Java Inter Process通信和Inter Thread通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.

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

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进程的字节序列的最低级别。您可以自己完成这项工作,也可以使用各种复杂程度的中间件技术来抽象出实现细节。可以使用的技术包括Java对象序列化,XML,JSON,RMI,CORBA,SOAP /Web服务,消息排队等。

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.

在实际水平上,线程间通信比进程间通信快许多个数量级,并且允许您更简单地完成许多事情。但缺点是所有内容都必须存在于同一个JVM中,因此存在潜在的可伸缩性问题,安全问题,健壮性问题等等。

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.

这篇关于Java Inter Process通信和Inter Thread通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆