Inner synchronization on the same object as the outer synchronization
Posted
by Yaneeve
on Stack Overflow
See other posts from Stack Overflow
or by Yaneeve
Published on 2010-03-17T15:15:11Z
Indexed on
2010/03/17
15:21 UTC
Read the original article
Hit count: 893
Recently I attended a lecture concerning some design patterns:
The following code had been displayed:
public static Singleton getInstance()
{
if (instance == null)
{
synchronized(Singleton.class) { //1
Singleton inst = instance; //2
if (inst == null)
{
synchronized(Singleton.class) { //3
inst = new Singleton(); //4
}
instance = inst; //5
}
}
}
return instance;
}
taken from: Double-checked locking: Take two
My question has nothing to do with the above mentioned pattern but with the synchronized block:
Is there any benefit whatsoever to the double synchronization done in lines 1 & 3 with regards to the fact that the synchronize operation is done on the same Object?
© Stack Overflow or respective owner