내가 읽고 있던 post on JavaCodeGeeks이 코드는 어떻게 스레드 안전합니까?
public User build() {
User user = new User(this);
if (user.getAge() > 120) {
throw new IllegalStateException(“Age out of range”); // thread-safe
}
return user;
}
및
public User build() {
if (age > 120) {
throw new IllegalStateException(“Age out of range”); // bad, not thread-safe
}
// This is the window of opportunity for a second thread to modify the value of age
return new User(this);
}
나는 후자는 아니지만 방법 하나는 이전의 코드가 스레드 안전하다고 말할 수있는 질문이
.
getAge()는 어떻게 생겼습니까? – SomeDude
@svasa 'age'가 현재 클래스의 멤버 인 경우 링크 –
의 게시물을 참조하십시오. 'build'는 인스턴스 메소드입니다. –