Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

jvm - In Java, are static class members shared among programs?

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Java, are static class members shared among programs?

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.

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).

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?

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.

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?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...