简体   繁体   English

在不同的 JVM 进程中运行的同一类的两个对象实例

[英]Two object instances of the same class running in separate JVM process

I have a simple Java SE application.我有一个简单的 Java SE 应用程序。 It creates two instances of the same class, but each one must run in a separate JVM process.它创建同一类的两个实例,但每个实例都必须在单独的 JVM 进程中运行。 How can achieve that?怎样才能做到这一点?

I have a simple Java SE application, it creates two instances of the same class, but each one must run in separate JVM processes, how can achieve that?我有一个简单的 Java SE 应用程序,它创建了同一个类的两个实例,但每个实例都必须在单独的 JVM 进程中运行,如何实现?

TL;DR:特尔;博士:

You cannot achieve this, and you should not be wanting this, as it has no sense at all.无法做到这一点,你也不应该想要这个,因为它根本没有任何意义。

Fetus can not be living in two different mothers' wombs.胎儿不可能生活在两个不同母亲的子宫里。 It has to belong to either.必须属于任何一个。


Little bit more:稍微多一点:

When your run a Java program, for each and single Java application (eg either just manually packaged .class files, a .jar file, J2EE Container, or any other Java application), a discrete instance of JVM spins up on top of your OS kernel.当您运行 Java 程序时,对于每个单独的Java 应用程序(例如,手动打包的.class文件、 .jar文件、J2EE 容器或任何其他 Java 应用程序),JVM 的离散实例会在您的操作系统之上旋转核心。

That JVM instance loads the corresponding bytecode, initializes Class Loader, allocates a memory and CPU time, and finally that's what you see as a running Java application .该 JVM 实例加载相应的字节码、初始化类加载器、分配内存和 CPU 时间,最后这就是您所看到的正在运行的 Java 应用程序

If you, however, are talking about the Inter Process Communication and confused this with what you ask in your question, then I have to tell you that IPC is an ability when two (or more) JVM instances communicate between each other , and not to share on Java main class among several JVMs.但是,如果您正在谈论进程间通信并将其与您在问题中提出的问题混淆,那么我必须告诉您,当两个(或更多)JVM 实例相互通信时,IPC 是一种能力,而不是在多个 JVM 之间共享 Java 主类。

As JLS §5.2 says :正如JLS §5.2 所说

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). Java 虚拟机通过使用引导类加载器(第 5.3.1 节)创建以实现相关的方式指定的初始类来启动。 The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). Java 虚拟机然后链接初始类,初始化它,并调用公共类方法 void main(String[])。

Hence, after bootstraping JVM, the main method is invoked after everything is linked and initialized.因此,在引导 JVM 之后,在链接和初始化所有内容后调用 main 方法。 You can not even theoretically share this among two ore more JVM instances.你甚至不能在理论上在两个或更多 JVM 实例之间共享这个。 Think about Class Loader, Garbage Collection, Linking Phase.. which should do what?想想类加载器、垃圾收集、链接阶段……应该做什么? which should bootstrap the main ?哪个应该引导主要 which should maintain Class type instances for object instantiation?哪个应该维护用于对象实例化的Class类型实例?

Also, each JVM instance provides single kernel-level Input and Output stream to communicate with OS.此外,每个 JVM 实例都提供单个内核级输入输出流以与操作系统通信。

You can, somehow, achieve the vice-versa by running more than one Java applications in one JVM, but that has a lot of implications and problems, and is very much discouraged to do.您可以以某种方式通过在一个 JVM 中运行多个 Java 应用程序来实现反之亦然,但这产生很多影响和问题,因此非常不鼓励这样做。

Last but not the least: Your System.in and System.out kernel-level I/O channels are the only instances from one JVM to the one OS Kernel.最后但并非最不重要的一点:您的System.inSystem.out内核级 I/O 通道是从一个 JVM 到一个 OS 内核的唯一实例。 Bear this in mind as well, as in case of multiple JVMs running single Java application, this is a complete mess and confusion.还要记住这一点,因为在多个 JVM 运行单个 Java 应用程序的情况下,这完全是一团糟和混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM