HashTable vs. HashMap

Keith Jones

Ranch Hand

Posts: 105

posted 10 years ago

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

Posts: 125

posted 10 years ago

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

Posts: 105

posted 10 years ago

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:

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?

SCJP 1.4

Jeroen T Wenting

Ranch Hand

Posts: 1847

posted 10 years ago

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

Posts: 105

posted 10 years ago

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

SCJP 1.4

Jeroen T Wenting

Ranch Hand

Posts: 1847

posted 10 years ago

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.

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

Posts: 105

posted 10 years ago

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.