P
Parvinder
public Object lookup(String jndiName) throws NamingException {
Object obj = homeInterfaces.get(jndiName);
if (obj == null)
{
synchronized (lock)
{
if ((obj = homeInterfaces.get(jndiName)) == null)
{
Context ctx = this.getInitialContext();
obj = ctx.lookup(jndiName);
homeInterfaces.put(jndiName,obj);
}
}
}
return obj;
}
The writer wanted to synchronize the entire block of the code.The
"lock" object is the object which is a member of the class. Getting a
lock on the object "lock" achieves the motive of synchronizing the
entire block but in my opinion it leaves a window to write some other
method which will can modify the homeInterfaces member (hash map) and
thus have a concurrency here.
According to me the code should be modified to
synchronized(homeInterfaces) which will essentially synchronize the
entire block of code as well as it will not leave any window to modify
the homeInterfaces hash map through some other function.
Any particular comments ?
~Parvinder
Object obj = homeInterfaces.get(jndiName);
if (obj == null)
{
synchronized (lock)
{
if ((obj = homeInterfaces.get(jndiName)) == null)
{
Context ctx = this.getInitialContext();
obj = ctx.lookup(jndiName);
homeInterfaces.put(jndiName,obj);
}
}
}
return obj;
}
The writer wanted to synchronize the entire block of the code.The
"lock" object is the object which is a member of the class. Getting a
lock on the object "lock" achieves the motive of synchronizing the
entire block but in my opinion it leaves a window to write some other
method which will can modify the homeInterfaces member (hash map) and
thus have a concurrency here.
According to me the code should be modified to
synchronized(homeInterfaces) which will essentially synchronize the
entire block of code as well as it will not leave any window to modify
the homeInterfaces hash map through some other function.
Any particular comments ?
~Parvinder