-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
오,, 이 방식은 처음 보는데,, 이런 방식도 있군요,, 😵 |
Beta Was this translation helpful? Give feedback.
-
|
싱글턴을 만들때 3) Lazy Initialization + Synchronization(지연 초기화 + 동기화) 방식으로 멀티스레딩의 문제를 해결하기 위해 사용한 synchronized로 인한 성능의 문제를 해결하는 방법으로
public class Singleton3 {
private volatile static Singleton3 uniqueInstance;
private Singleton3() {}
public static Singleton3 getInstance() {
if (uniqueInstance == null) {
synchronized (Singleton3.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton3();
}
}
}
return uniqueInstance;
}
}
이번 아이템을 통해 싱글턴을 만드는 여러 방법에 대해 알게 되었고 싱글턴을 만들게 됨으로써 생기는 문제점을 해결하기 위해 각 방식이 생겨나게 된 이유도 알게 되었습니다. 정리 감사합니다. |
Beta Was this translation helpful? Give feedback.
-
|
정리 해주신 글 잘 읽었습니다 👍 public class Singleton { // Lazy loading with a holder
private static class SingletonHolder {
static {
System.out.println("In SingletonHolder static block.");
}
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
System.out.println("In getInstance().");
return SingletonHolder.INSTANCE;
}
private Singleton() {
System.out.println("In constructor.");
}
private void doSomething() {
System.out.println("Singleton working.");
}
public static void main(String[] args) {
System.out.println("Start of main.");
Singleton.getInstance().doSomething();
System.out.println("End of main.");
}
}
public class Singleton { // Eager loading without a holder
static {
System.out.println("In Singleton static block.");
}
private static final Singleton INSTANCE = new Singleton();
public static Singleton getInstance() {
System.out.println("In getInstance().");
return INSTANCE;
}
private Singleton() {
System.out.println("In constructor.");
}
private void doSomething() {
System.out.println("Singleton working.");
}
public static void main(String[] args) {
System.out.println("Start of main.");
Singleton.getInstance().doSomething();
System.out.println("End of main.");
}
}
|
Beta Was this translation helpful? Give feedback.

싱글턴을 만들때 3) Lazy Initialization + Synchronization(지연 초기화 + 동기화) 방식으로 멀티스레딩의 문제를 해결하기 위해 사용한 synchronized로 인한 성능의 문제를 해결하는 방법으로
4)LazyHolder 방식을 사용 하였는데 저도 처음 알게 되었습니다.3)문제를 해결하는 방법으로4)말고도 알고있는 내용으로는DCL(Double Checked Locking) 방식에 대해 알고 있습니다.
3)방식의 문제는 동기화가 꼭 필요한 시점은 …