Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

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.synchronizedMap ,

 i can achieve this functionality using Hashtable also;

like this
Hastable ht = new Hastable();

by default hashtable gives synchronization; why they have written Collections.synchronizedMap(new HashMap());

whta benefits will it give?

 homes       =       Collections.synchronizedMap(new HashMap());

public class EJBHomeFactory
{
  private Map         homes;
  private static  EJBHomeFactory singleton;
  private Context ctx;

  private EJBHomeFactory() throws NamingException
  {
          homes       =       Collections.synchronizedMap(new HashMap());
          ctx       =       new InitialContext();

  }

  public static EJBHomeFactory getInstance() throws NamingException
  {
      if (singleton == null)
      {
        singleton = new EJBHomeFactory();
      }
    return singleton;
  }
}
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
Its there if you want a synchronized Map that uses a *different* Map implementation, such as HashMap.
Avatar of chaitu chaitu

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.synchronizedMap(new HashMap());


singleton and synchronized are not really related.
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.