-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
아이템 83. 지연 초기화는 신중히 사용하라 #85
Comments
[83] 지연 초기화는 신중히 사용하라지연 초기화
지연 초기화 특징
초기화 방법1. 일반적인 초기화 방법private final FieldType field = computeFieldValue();
2. 지연 초기화 - synchronized 한정자
private FieldType field;
private synchronized FieldType getField() {
if (field == null) {
field = computeFieldValue();
}
return field;
} 3. 정적 필드 지연 초기화 홀더 클래스 관용구
private static class FieldHolder {
static final FieldType field = computeFieldValue();
}
private static FieldType getField() {
return FieldHolder.field;
}
4. 인스턴스 필드 지연 초기화 이중검사 관용구
private volatile FieldType field;
private FieldType getField() {
FieldType result = field;
// 첫번째 검사 (Lock 사용 안함)
if (result != null) {
return result;
}
// 두번째 검사 (Lock 사용)
synchronized(this) {
if (field == null) {
field = computeFieldValue();
}
return field;
}
}
5. 이중검사 관용구의 변종 - 단일 검사 관용구
private volatile FieldType field;
private FieldType getField() {
FieldType result = field;
if (result == null) {
field = result = computeFieldValue();
}
return result;
} 6. 이중검사 관용구의 변종 - 짜릿한 단일 검사 관용구 (racy single-check)
private FieldType field;
private FieldType getField() {
FieldType result = field;
if (result == null) {
field = result = computerFieldValue();
}
return result;
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: