Is there any significant difference between using a Hashtable (always synchronized) and using Collections.synchronizedMap(HashMap)???
- Posted by: Niklas Eriksson
- Posted on: November 05 2001 05:13 EST
Please give me your opinions about when to use one or the other.
- synchronized hashMap or Hashtable? by Gal Binyamini on November 05 2001 18:20 EST
- synchronized hashMap or Hashtable? by Javin Paul on January 05 2011 09:56 EST
IMO, the important thing is that in most of your code (except initialization) you use Map. This gives you the freedom to select different implementations as needed.
If you need a hash map and you use Map, I think there is really no notable difference between the two classes you mentioned.
If you happen to need iterators that are not fail-fast, you can use Hashtable and it's Enumeration. I prefer to avoid this approach unless it is really necessary, because it is non-standard and uses "old-style" iteration (Enumeration).
Just my 2 cents
Would it cause me any problems (according to performance etc) - to use synchronization in my EJBs - like Hashtables or explicit synchronization of my methods (EJB methods or in objects that are used by my EJB).
Is it correct to assume that there will only be one thread working on my EJB at the time?
Yes, there will be only one thread in your EJB at any point of time. This is guaranteed by the specs.
Regarding your synchronization question, it really depends on how you use it & what load is put on the server. If you have a singleton class being used by all EJBs & with each singleton method "synchronized" you'll see some performance hit when the load is increased. This may or may not be significant depending on your application.
The synchronized collections classes, Hashtable and Vector, and the synchronized wrapper classes, Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List.
However, several factors make them unsuitable for use in highly concurrent applications -- their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationExceptions.
The ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use HashMap or ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use.
to read more <a href="Hashtable" rel="nofollow">http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html">Hashtable vs HashMap </a><a href="Hashtable" rel="nofollow">http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html">Hashtable vs HashMap </a>