跨 Java 进程共享对象

发布于 2024-08-04 13:14:10 字数 365 浏览 9 评论 0原文

我正在从主应用程序执行另一个 JVM (java.exe)。有没有办法与新创建的进程(在创建时或创建后)共享对象(相当大的对象)。

someObject sO= new someObject();

//sO is populated

//Creating new process

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java  -cp " + tempDir +  jarsInPath  + " " + appMain);

现在我希望 sO 对象可供 proc 对象表示的进程使用

ProcessBuilder 是否为此目的提供任何实用程序?

I am executing another JVM (java.exe) from the main application. Is there any way to share an object (rather large object) with the newly created process (at the time of creation or after it was created).

someObject sO= new someObject();

//sO is populated

//Creating new process

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java  -cp " + tempDir +  jarsInPath  + " " + appMain);

Now I want the sO object to be available to the process denoted by the proc object

Does ProcessBuilder provide any utilities for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戏剧牡丹亭 2024-08-11 13:14:10

如果要共享对象,最好的方法是使用线程而不是单独的进程。进程无法共享内存(通过 JNI 除外),因此您必须通过文件或 RMI 套接字连接以序列化形式来回复制大对象(后者是更好的选择,因为它会导致固有同步) 。

If you want to share objects, the best way is to use threads instead of a separate process. Processes cannot share memory (except through JNI), so you'd have to copy the large object back and forth in serialized form, either via files or via RMI socket connection (with the latter being the better option since it results in inherent synchronization).

被你宠の有点坏 2024-08-11 13:14:10

您可以公开服务以允许从对象访问数据。使用 RMI 建立进程间通信相对简单。将会有 IPC 开销,因此这不会像本地访问那样具有性能,细粒度访问会变得昂贵,但如果您正在获取摘要或其他聚合数据,那么这可能是一个不错的模型。

您没有说明为什么这些是单独的过程。您是否有机会将子进程的代码直接加载到父进程中?动态加载和卸载是可能的。

You can expose a service to allow access to the data from the object. It's comparatively simple to set up inter-process communication using RMI. There's going to be an IPC overhead so this will not be as performant as local access, fine-grained access will get expensive, but if you're getting summary or other agregated data then this could be a decent model.

You don't say why these are separate processes. Do you have any opportunity to load the code of you child process directly into the parent? Dynamic loading and unloading is possible.

一袭水袖舞倾城 2024-08-11 13:14:10

不,Java 中不支持共享内存。

解决这个问题的最简单方法是将对象序列化到临时文件中,然后在新的 JVM 中将其反序列化。

No there is no shared memory support in Java.

The simplest way to tackle this would be to serialize the object into a temp file and then deserialize it back in the new JVM.

三月梨花 2024-08-11 13:14:10

我认为你可以使用分布式缓存来达到这个目的(EHCache、memcached 等等......)

I'm think you can use distributed caches for this purposes (EHCache, memcached and so on...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文