We will make our code synchronized (using synchronized method)
public static synchronized Singleton getInstance() {
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
We can optimize above code using synchronized block
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
singleton = new Singleton();
}
}
return singleton;
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.