在 Java 中,静态类成员是否在程序之间共享?

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

我想不,他们不是,因为每个进程当然都有自己的内存空间.

但是整个 JVM 到底是如何工作的呢?对于我启动的每个 Java 程序,在单独的进程中是否有单独的 JVM?在系统中运行的 Java 程序是否共享任何东西?操作系统和 JVM 实现之间有区别吗?我可以make 程序共享变量吗(即直接通过 JVM 而不是通常的 IPC 机制)?是否有更多用于特殊用途的奇异单进程 JVM?

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all? Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

一般来说,关于 JVM 内核的推荐读物有哪些?规范?一些实现的源代码?网站?书籍?

Generally, what are recommendable reads about the guts of JVMs? The spec? The source code of some implementation? Websites? Books?

推荐答案

在 Java 中,是静态类成员在程序之间共享?

In Java, are static class members shared among programs?

一个类由它的全名和加载它的类加载器定义.如果同一个类在同一个 JVM 进程中,并且两个程序通过同一个类加载器加载该类,那么静态成员是共享的.类加载规则极其重要.

A class is defined by its full name and the class loader that loaded it. If the same class is within the same JVM process and the two programs loaded the class through the same class loader then the static members are shared. Classloading rules are of extreme importance.

我想不,他们不是,因为每个过程都有自己的当然是内存空间.

I imagine that no, they aren't, because every process has its own memory space, of course.

如果您使用两个独立的 JVM 来启动两个应用程序,那么您是正确的.但是以应用程序/servlet 容器为例,例如 tomcat:它们通过同一个进程(tomcat 宿主进程)加载多个应用程序.

If you are using two separate JVMs to launch two apps, you are correct. But take the case of application/servlet containers such as tomcat: they load several apps through the same process (the tomcat host process).

但是整个 JVM 是怎么回事实际工作?是否有单独的 JVM在每个 Java 的单独进程中我启动的程序?做Java在系统共享中运行的程序有什么吗?

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all?

每次您在命令行键入 >java -cp... 时,您都在创建一个新进程.请记住,当您运行 eclipse 或使用 fork=true 调用 ant java 任务时,您也在创建新进程.

Every time you type >java -cp... at the command line, you are creating a new process. Bear in mind that when you run eclipse or call an ant java task with fork=true you are also creating new processes.

操作系统和操作系统之间有区别吗?JVM实现?我可以做吗程序共享变量(即直接通过JVM而不是通常的 IPC 机制)?在那里更奇特的单进程 JVM特殊用途?

Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

正如海报所说,有像 Terracota 这样的项目可以为您提供便利.这种共享的一般方法是分布式缓存.

Like a poster said, there are projects like Terracota that facilitate this for you. A general approach for this kind of sharing is a distributed cache.

相关文章