Look at the clone operation of Java from the perspective of the Java virtual machine

Recently, a downgrade strategy has been added to the fuse component (Hystrix does not seem to have this configuration). We provide the following strategies:

1. The default strategy

2. Return a constant value

3. Throw a specified exception

4. Execute a groovy script

Of course, these configurations can be configured on the platform and take effect immediately.

The current implementation of returning constant values ​​is as follows:

Look at the clone operation of Java from the perspective of JVM

Today I discovered that if the same object is returned every time, and this logic is a black box for the business (the business does not know that the object it gets each time is the same), if the object is operated on, it will definitely affect For other requests, we need to return a new object each time in order to avoid back-ups. The first reaction is whether cloning is feasible (because each json serialization will also lose performance), but the clone method that comes with jdk is only shallow Clone, if the object contains another complex object, the cloned object still has the risk of being modified.

You can look at the following example:

 

class Master {
 String name;
 public Master(String name) {
 this.name = name;
 }
}

Initialize a Master class

 

class Dog implements Cloneable {
 String name;
 int age;
 Master master;
 public Dog(String name, int age, Master master) {
 this.name = name;
 this.age = age;
 this.master = master;
 }
 @Override
 protected Object clone() throws CloneNotSupportedException {
 return super.clone();
 }
 @Override
 public String toString() {
 return "{name:" + name + ", age: " + age + ", master: "+ master.name + "}";
 }
}

Initialize another dog

 

public static void main(String[] args) throws Exception {
 Master master = new Master("zj0");
 Dog dog1 = new Dog("旺财", 1, master);
 Dog dog2 = (Dog)dog1.clone();
 dog1.name = "比利";
 dog1.master.name = "zj1";
 System.out.println(dog2);
}

Finally run it, the results are as follows:

 

{name:旺财, age: 1, master: zj1}

Dog1 is the original dog, and dog2 is cloned, but when I modify the name of the master of dog1, the cloned master also changes, which obviously doesn't work.

Although I have always known that the clone method of Object is a shallow clone, I have not continued to explore it. I will take a look at the implementation of JVM today. It seems very simple. In the jvm.cpp file, search for "JVM_Clone"

Look at the clone operation of Java from the perspective of JVM

I have never used the clone method before. I found through the source code that it will check whether the class implements the Cloneable interface when it is running. It is not checked during compilation. What do you think?

According to the size of the object or data, a piece of memory of the same size is opened from the heap, and then the data of the original object is copied to the new memory address. For basic types, the original value can be copied, but for internal objects, its What is saved is only an address, and the address is also copied when copying, and eventually it points to the same object, which causes the above-mentioned problems.


 

Guess you like

Origin blog.csdn.net/mrchaochao/article/details/108625867