내 웹 응용 프로그램에 "Foo"클래스라는 클래스가 있다고 가정 해 봅니다. Spring을 사용하여 bean을 생성 할 때 호출되는 initialise() 메소드가있다. 그런 다음 initialise() 메서드는 외부 서비스를로드하여 필드에 할당하려고합니다. 서비스에 접속할 수없는 경우 필드는 null로 설정됩니다.Java 웹 응용 프로그램 동기화 질문
private Service service;
public void initialise() {
// load external service
// set field to the loaded service if contacted
// set to field to null if service could not be contacted
}
누군가가 초기화() 메소드에서 시작 된 경우 서비스가 호출되는 클래스 "푸"에서의 메소드 GET()를 호출합니다. 서비스 필드가 null 인 경우 외부 서비스를로드하려고합니다.
public String get() {
if (service == null) {
// try and load the service again
}
// perform operation on the service is service is not null
}
이와 같은 작업을 수행하면 동기화 문제가 발생할 수 있습니까?
고마워요! 리팩토링과 함께 좋은 아이디어;) – digiarnie
그것은 이중 검사 잠금의 한 형태이며, 따라서 : 1. 이것은 Java 5 이상에서만 작동합니다. 2. "서비스"는 휘발성으로 선언되어야합니다. –
참조 : http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html –