The String class does not provide a method to change the contents of a string. The content of the string represented by the String instance never changes. For this reason, the methods of the String class do not need to be synchronized. ** Immutable ** means "immutable" or "unchanged". In the Immutable pattern, a class (** immutable class **) that guarantees that the state of the instance does not change appears. Since exclusive control is not required, performance improvement can be expected if used well.
(See this manual for the entire code)
public final class Person {
private final String name;
private final String hobby;
public Person(String name, String hobby) {
this.name = name;
this.hobby = hobby;
}
public String getName() {
return name;
}
public String getHobby() {
return hobby;
}
}
In this class, the value of the field of Person class can be set only in the constructor. There is also no method to change the value of the field. Therefore, once an instance of the Person class is created, the value of the field does not change. At this time, the Person class is safe even if it is accessed from multiple threads at the same time. Neither method needs to be synchronized.
** Immutable role ** appears. The Immutable role is a class that cannot change the value of a field and does not have a method to change the contents of the field.
--When the state does not change after the instance is created. It is the following time. ** The field is final, the setter method does not exist, and the instance referenced by the field does not change **.
Note that the following classes are not immutable.
public final class Hoge {
private final StringBuffer hoge;
public Hoge(String a, String b) {
this.hoge = new StringBuffer("a=" + a + ", b=" + b);
}
public StringBuffer getHoge() {
return hoge;
}
}
The instance that the hoge field obtained by the getHoge method has is not a String but an instance of StringBuffer. Since the StringBuffer class has a method to change the internal state, the contents of the hoge field can be rewritten from the outside. Since the hoge field is declared final, the value of the field itself does not change, but the state of the instance to which the field (think of it as a pointer) points may change.
--When the instance is shared and frequently accessed.
Some Java standard class libraries are a pair of mutable and immutable classes. For example, the StringBuffer and String classes. StringBuffer is properly synchronized when rewriting. On the other hand, the String class does not use Synchronized, so high-speed reference is possible. Use StringBuffer if you want to change the content frequently, and use String if you don't need to change the content and just refer to it.
If you remove synchronized on the assumption that it is an Immuable pattern, the safety of the class will be lost as soon as the immutability is lost. Therefore, invariance should be clearly stated in the program comments and API documentation.
Relation Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 1) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 2) Summary of "Design Patterns Learned in Java Language (Multithreaded Edition)" (Part 3)
Recommended Posts