benefits of Collections.synchronizedMap(new HashMap()) over Hashtable

Posted on 2004-11-19
Last Modified: 2009-12-16


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;
Question by:chaitu chaitu
    LVL 13

    Accepted Solution

    >>  homes      =      Collections.synchronizedMap(new HashMap());
    Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.

    which means saftly more than one thread can update/read ur hashtable
    LVL 92

    Expert Comment

    Its there if you want a synchronized Map that uses a *different* Map implementation, such as HashMap.
    LVL 20

    Author Comment

    by:chaitu chaitu
    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());

    LVL 92

    Expert Comment

    singleton and synchronized are not really related.
    Singltons still need to be correctly synchronized if access by mutiple threads.
    LVL 92

    Expert Comment

    > Returns a synchronized (thread-safe) map backed by the specified map.

    But you get that already with a Hashtable.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now