JVM 是否可以同时运行多个程序?

2022-01-16 00:00:00 jvm java

JVM 可以同时运行多个程序吗?如果是这样,怎么做?如果不是,为什么?

Is it possible for a JVM to run more than one program at the same time? If so, how? If not, why?

要运行程序,我们只需这样做

To run a program, we simply do

java ProgramName

但是我们可以使用同一个 JVM 实例来运行另一个程序吗?

But can we use the same JVM instance to run another program?

推荐答案

答案取决于你对程序"的定义.具有 main 方法并且以 java NameOfClass 开头的 Java 程序通常不能在同一个 JVM 中运行,因为没有内置的资源或命名空间分离.例如,如果两个程序使用相同库的冲突版本怎么办?

The answer depends on your definition of "program." The Java programs that have a main method and that you start with java NameOfClass typically cannot be run in the same JVM because there's no builtin separation of resources or namespaces. For example, what if the two programs use conflicting versions of the same library?

我们还有旨在共享 JVM 的应用程序,例如企业应用程序.这些程序旨在在应用程序服务器"的上下文中运行,该程序基本上是用于运行其他程序的程序.应用程序服务器使用类加载器和安全管理器将应用程序资源分开.例如,由于通过不同的类加载器加载,两个应用程序可能使用冲突的库版本或冲突的类名,但仍然共享一个 JVM.

We also have applications that are designed to share a JVM, such as enterprise applications. These are programs that are designed to run in the context of an "application server", which is basically a program for running other programs. An application server keeps the applications resources separate using classloaders and security managers. For example, two applications may use conflicting versions of a library or conflicting class names and still share a JVM thanks to being loaded through different classloaders.

相关文章