简体   繁体   English

JVM是否在两个单独的实例中实现了不同的应用程序?

[英]Does the JVM make two separate instances of classes that are implemented in different applications?

Imagine two simple java applications. 想象一下两个简单的java应用程序。 Both of them are implementing the same JAR file containting an enum file like this: 它们都实现了包含这样的枚举文件的相同JAR文件:

enum enum1{
   value1;
}

In both the applications I print the enum1.value1´s hashcode. 在这两个应用程序中,我打印了enum1.value1的哈希码。

System.out.print(enum1.value1.hashCode());

How does the JVM work since the hashcodes are not equal even if the JAR file that the two applications implement is the same? 即使两个应用程序实现的JAR文件相同,JVM如何工作,因为哈希码不相等?

Why are not the hashcodes equal since it is the same JAR file the both applications implement? 为什么哈希码不相等,因为它们是两个应用程序实现的相同JAR文件?

EDIT 编辑
I have two applications that access a database. 我有两个访问数据库的应用程序。 They are run within the same JVM. 它们在同一个JVM中运行。 I would like to implement a locking mechanism so that when application 1 is writing to the database, application 2 has to wait for its turn (that is when the lock has been released by application 1). 我想实现一个锁定机制,以便当应用程序1写入数据库时​​,应用程序2必须等待轮到它(即应用程序1释放锁定时)。 My solution is, if possible, to make an interface that has some ReentrantLocks declared or an enum that should act as lock and is used by the two applications. 如果可能的话,我的解决方案是创建一个声明了一些ReentrantLocks的接口或一个应该充当锁的枚举,并由两个应用程序使用。 But then the instance of the interface/enum should be equal in both the applications, because you can only synchronize the same object. 但是接口/枚举的实例在两个应用程序中应该相等,因为您只能同步同一个对象。

EDIT 2 编辑2
This is the architecture: 这是架构:

App1.jar              Commons.jar         App2.jar
App1Main.class        Commons.class       App2Main.class 

Both App1 and App2 includes Commons.jar. App1和App2都包含Commons.jar。 The commons.class is just a simple singleton class. commons.class只是一个简单的单例类。 In both App1 and App2 I print the commons instance hashcode: 在App1和App2中,我打印公共实例哈希码:

System.out.println(Commons.getInstance().hashCode());

Both java applications are run like "java -jar app1" and "java -jar app2" so there are two processes when they are running. 两个java应用程序都像“java -jar app1”和“java -jar app2”一样运行,因此运行时有两个进程。

But they print different hashcodes and that is, what I belive (correct me if I am wrong), because they have been loaded by different class loaders. 但是他们打印了不同的哈希码,也就是我所相信的(如果我错了,请纠正我),因为它们已被不同的类加载器加载。 But the mystic arrives when I print the classloader in both apps: 但是当我在两个应用程序中打印类加载器时,神秘主义者到了:

System.out.print(ClassLoader.getSystemClassLoader().hashCode());

Then the hashcode is equal in both applications. 然后,两个应用程序中的哈希码相等。

It's not clear whether you're talking about two applications within the same process or not. 目前尚不清楚您是否在同一流程中讨论两个应用程序。 Even if you are, if those two applications have separate ClassLoader instances loading the same jar file, the two enum types are different types as far as the JVM is concerned. 即使你是这样,如果这两个应用程序有单独的ClassLoader实例加载相同的jar文件,那么就JVM而言,两个枚举类型是不同的类型 If you want a single type within a process used by two applications, it has to be loaded by a single classloader. 如果您想在两个应用程序使用的进程中使用单个类型,则必须由单个类加载器加载。

See the Javadoc for Object.hashCode() 请参阅Javadoc for Object.hashCode()

The general contract of hashCode is: hashCode的一般契约是:

  1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer , provided no information used in equals comparisons on the object is modified. 每当在执行Java应用程序期间多次同一对象上调用它时,hashCode方法必须始终返回相同的整数 ,前提是不修改对象的equals比较中使用的信息。 This integer need not remain consistent from one execution of an application to another execution of the same application. 从应用程序的一次执行到同一应用程序的另一次执行,该整数不需要保持一致。

  2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 如果两个对象根据equals(Object)方法相等,则对两个对象中的每一个调用hashCode方法必须生成相同的整数结果。

  3. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. 如果两个对象根据equals(java.lang.Object)方法不相等,则不需要在两个对象中的每一个上调用hashCode方法必须生成不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 但是,程序员应该知道为不等对象生成不同的整数结果可能会提高哈希表的性能。

Also

This is typically implemented by converting the internal address of the object into an integer 这通常通过将对象的内部地址转换为整数来实现

So your result seems perfectly legitimate to me. 所以你的结果对我来说似乎是合法的。 If you need the hash code to be consistent, maybe you are doing it wrong, since the API is not intended to be used this way. 如果您需要哈希码是一致的,那么您可能做错了,因为API不打算以这种方式使用。 Maybe you could tell your objective. 也许你可以告诉你的目标。

Think of JAR as static resource containing a recipe for an application which may be run. 可以将JAR视为包含可以运行的应用程序的配方的静态资源 When the application is actually run, all the resources are loaded into computer's memory using rules specific for an operating system. 实际运行应用程序时,所有资源都使用特定于操作系统的规则加载到计算机的内存中。 These rules does not enforce classes to be loaded in the same virtual memory address. 这些规则不强制要在同一虚拟内存地址中加载类。

enum is just a key word which makes the declared type class(For example enum Day means you will be having a class like Day extends Enum), class is a simple Template to the object which will be constructed in JVM which has its own hash code so obviously when you create a new Object you will get new hash code that is the reason why you are getting new hash code reffer following link for more details.... 枚举只是一个关键词,它使得声明的类型类(例如枚举日意味着你将拥有像Day extends Enum这样的类),类是一个简单的对象模板,它将在JVM中构造,它有自己的哈希码很明显,当你创建一个新的对象时,你会得到新的哈希码,这就是为什么你得到新的哈希码reffer跟随链接的更多细节....

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

observe "All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else" this point clearly. 观察“所有枚举隐式扩展java.lang.Enum。由于Java不支持多重继承,因此枚举不能扩展其他任何东西”这一点很清楚。

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

相关问题 通过两个不同的JVM进程将Hibernate实例分开 - Separate Hibernate instances by two different JVM processes anomaly JVM在哪里存储在运行时实现的抽象类的信息? - Where does JVM store information of abstract classes implemented at runtime? 在不同的 JVM 进程中运行的同一类的两个对象实例 - Two object instances of the same class running in separate JVM process 在不同的eclipse实例中调试tomcat中的两个应用程序 - debug two applications in tomcat in different eclipse instances 在单独的类中实现时,JLabel 不会出现在 JFrame 中 - JLabel does not appear in JFrame when implemented in separate classes 在不同的JVM版本下运行两个Fuse实例 - Running two instances of Fuse each under a different JVM version 在企业应用程序中同时使用 hibernate 和 spring 时,是否创建了两个单独的 JVM 实例? - Are there two separate JVM instances created when both hibernate and spring are used in an enterprise application? Java JVM 是否像 String 一样重用自定义类的相同不可变实例? - Does Java JVM reuse identical immutable instances of custom classes like it does with String? 在Web应用程序中重用已实现的业务类 - Reuse implemented business classes in a web applications 石英是否为不同的石英实例创建一个单独的线程池? - Does quartz create a separate threadpool for different quartz instances?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM