Имам статичен клас "Builder" в клас, наречен "MyClass". Ако се опитам да създам два екземпляра на MyClass, използвайки конструктора едновременно от две нишки, ще бъде ли безопасно? Могат ли стойностите, зададени от една нишка, да бъдат присвоени на обект, създаден от друга нишка?
Код:
public class MyClass {
private int height;
private int weight;
private MyClass(Builder builder) {
height = builder.height;
weight = builder.weight;
}
public static class Builder {
private int height;
private int weight;
public Builder height(int h) {
height = h;
return this;
}
public Builder weight(int w) {
weight = w;
return this;
}
public MyClass build() {
return new MyClass(this);
}
}
}