File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes HashTable vs. HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "HashTable vs. HashMap" Watch "HashTable vs. HashMap" New topic
Author

HashTable vs. HashMap

Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
The map that we create to hold our locked records can be a HashMap or a HashTable (it could be something else as well). However, in the update method for example, if we use HashMap (which is synchronised) we would have to do something like this:

synchronised (map) {
while(isLocked(recNo)) {
map.wait();
}
...
}

If we use the unsychronised HashTable though do we need to have this block?



SCJP 1.4
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Hashtable is synchronized, not unsynchronized.

Personally, I'd avoid using Hashtable. Rather use Collections.synchronizedMap()

Jeremy


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Ok oops I got that the wrong way around.

1. So, if HashTable is synchronised why would I bother with gettting a synchronised version of a non-synchronised map? I assume you are suggesting doing this:

Map m = Collections.synchronizedMap(new HashMap()); // where HashMap is not synchronised

2. In the case where I use a HashTable or the Collections.synchronizedMap method I get a synchronised map.

a) But why is it that the documentation for Collections.synchronizedMap that we must still do this:

Set s = m.keySet(); // Needn't be in synchronized block
...
synchronized(m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}

That is why do we still need a synchronised block? I guess there are four additional questions here:

b) How do we know we need a synchronised block for a synchronised datastructure?
c) How do we know we don't need a synchronised block for a synchronised datastructure?
d) How do we know we need a synchronised block for a non-synchronised datastructure?
e) How do we know we don't need a synchronised block for a non-synchronised datastructure?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Hashtable is a legacy class which is mainly retained for backwards compatibility and to make sure authors of schoolbooks don't have too much work answering questions from kids who find their code no longer compiles when they install a JDK later than 1.1.

In other words, avoid it if possible.
Same for Vector and StringTokenizer, as well as ANY deprecated functionality.
[ November 16, 2006: Message edited by: Jeroen T Wenting ]

42
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Thanks for the reply,

I certainly will avoid HashTable. Let me rephrase my other question succinctly. What's the benefit of using Collection.synchronizedMap(new HashMap()) to just HashMap?

Thanks again
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
If you use that you'll get a Map back which guards itself against simultaneous access by multiple threads (instead of you having to guard against it by only accessing it in a synchronized environment).

That's all really. If you don't need the synchronisation (like clientside most likely and serverside also depending on your implementation) you may never need it at all.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

To try and answer your questions of this form:
b) How do we know we need a synchronised block for a synchronised datastructure?
A "synchronized datastructure", at least as far as Hashtable goes, is simply a class all of whose methods are declared "synchronized". That's all. If you need to synchronize other operations, as in your original post, synchronizing all of the class's methods does nothing to help you with that. Or to hinder it, for that matter. You still have to synchronize those other operations regardless.
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Thanks for that. All this is beginning to fall into place now for me. I recently bought the Monkhouse book which is great and I've got to the IceCreamMan section. It's really making sense now. I do appologise for asking such basic questions. I've also purchased head first Design Patterns. Another great book. My reason for asking so many stupid questions is that I have not actually ever written more than a few days of code in a commercial environment - all that I know of Java is from University and private study and therefore my knowledge is incomplete. I only got 86% in the SCJP compared to the high 90s that some of you guys acheived.

Slowly but surely ...

I'm gratefull for your help.
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Oops I wrote:

"... ever written more than a few days of code ..."

when I meant:

"... ever written more than a few days of Java code ..."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashTable vs. HashMap
 
Similar Threads
HashMap Vs HashTable
synchroniztion of map
Need your help
Difference between hashtable and hash map
Array of Hashtables :-How to display