chaitu chaitu
asked on
benefits of Collections.synchronizedMap(new HashMap()) over Hashtable
hi
this is our EJBHomeFactory class ;we have implemented singleton pattern for this;
my doubt is
synchronizing the hashmap using Collections.synchronizedMa
i can achieve this functionality using Hashtable also;
like this
Hastable ht = new Hastable();
by default hashtable gives synchronization; why they have written Collections.synchronizedMa
whta benefits will it give?
homes = Collections.synchronizedMa
public class EJBHomeFactory
{
private Map homes;
private static EJBHomeFactory singleton;
private Context ctx;
private EJBHomeFactory() throws NamingException
{
homes = Collections.synchronizedMa
ctx = new InitialContext();
}
public static EJBHomeFactory getInstance() throws NamingException
{
if (singleton == null)
{
singleton = new EJBHomeFactory();
}
return singleton;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Its there if you want a synchronized Map that uses a *different* Map implementation, such as HashMap.
ASKER
i have another doubt
class EJBHomeFactory is itself is a singleton meena only single instance at a time is possible;
again why we r making making map as synchronized
using this:
homes = Collections.synchronizedMa p(new HashMap());
class EJBHomeFactory is itself is a singleton meena only single instance at a time is possible;
again why we r making making map as synchronized
using this:
homes = Collections.synchronizedMa
singleton and synchronized are not really related.
Singltons still need to be correctly synchronized if access by mutiple threads.
Singltons still need to be correctly synchronized if access by mutiple threads.
> Returns a synchronized (thread-safe) map backed by the specified map.
But you get that already with a Hashtable.
But you get that already with a Hashtable.