Preface
Recently, we have added a downgrade strategy to the fuse component (Hystrix does not seem to have this configuration), and we provide the following strategies:
1. Default policy
2. Return 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:
Today I found 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 obtained each time is the same), if the object is operated on, it will definitely affect other requests. In order to avoid taking the blame, we need to return a new object every time. The first reaction is whether it is feasible through clone (because each JS serialization will also lose performance), but the clone method that comes with jdk is only a shallow clone. If the object contains another complex object, the object derived from clone still has the risk of being modified.
Detailed introduction:
You can see 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 a dog
public static void main(String[] args) throws Exception { Master master = new Master("zj0"); Dog dog1 = new Dog("Wangcai", 1, master); Dog dog2 = (Dog)dog1.clone(); dog1.name = "Billy"; dog1.master.name = "zj1"; System.out.println(dog2);}Finally run it, the result is as follows:
{name:王彩, age: 1, master: zj1}dog1 is a primitive dog, dog2 is cloned, but when I modify the name of dog1's master, the cloned master also changed, which obviously doesn't work.
Although I have always known that the clone method of Object is a shallow clone, and I haven't continued to explore it. Today I came across it and took a look at the implementation of JVM. It seems very simple. In the jvm.cpp file, search for "JVM_Clone"
I have never used the clone method before. Through the source code, I found that when running, I check whether the class implements the Cloneable interface. I don’t check it during compilation. What do I think?
According to the size of the object or data, open a piece of memory of the same size from the heap, and then copy the data of the original object to the new memory address. For basic types, the original value can be copied over, but for internal objects, it only saves an address, and it is also a copy of the address during copying, and ultimately points to the same object, which causes the above problems.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.