Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hash Table - Sychronization

 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Hash Table is synchronized ?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No particularly good reason. It probably seemed like a good idea at the time, but in practice, even when you need multi-threaded access to a hash table, synchronization is usually needed on coarser grained application-level methods rather than the little Hashtable methods. All it does is lure inexperienced developers into thinking their code is threadsafe because the Hashtable they use is threadsafe. It isn't.

This is why, when the Collections framework was introduced in JDK 1.2, none of its classes were synchronized. This is also one of the reasons why you should always prefer HashMap over Hashtable and ArrayList over Vector. God only knows why everyone still loves to use an old, bloated, misconceived, superfluous legacy class like Vector.

- Peter
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not God only - me too.

Lot of people learned about Vector and Hashtable before 1.2, and in cases, where you don't use Threads - and many people don't - they work perfectly.

Why learn something new, if it is more useful only in rare circumstances?
Vector is in head, ArrayList in Javadocs. Compare the accesstime: lower than 1 sec. - greater than one minute.

Early vector-learners wrote books, tutorials and became teacher, and so the use of - not really deprecated - techniques is traded to newcomers, and will only slowly fade away.

And there is so much 'don't do this - do that' - noise in forums, that it is often wise to ignore advice .
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lot of people learned about Vector and Hashtable before 1.2

Including me, and Peter. Yes, we too once walked in darkness as you do now, Stefan. But one day we read the Epistile of the Prophet Joshua to the Philistines, and saw the Light. Now we seek to share this knowledge with others, that they may know true happiness. Or failing that, for those who reject the teachings of Joshua, we must strive to prevent them from spreading their vile corruption to others.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
Why learn something new, if it is more useful only in rare circumstances? Vector is in head, ArrayList in Javadocs.
O ye philistine, doth this mean that thou dost access the Vector through any other path than the List interface? And if thou doest this as the Prophet Joshua hath decreed, what avail thee the javadocs?

- Peter

PS. Does this mean that you access the Vector through anything other than the List interface? And if you do, as is best practice, what do you need to consult the javadocs for?

PS2. I strongly disagree with the "only rarely useful" bit.
[ July 27, 2004: Message edited by: Peter den Haan ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whyfor art thou walking in the wilderness of JDK 1.1 and earlier? For lo - we have been brought to the APIs of plenty.

(In other words, ignoring the new classes will limit your options, and can often lower your codes performance / capabilities).

Regards, Andrew
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we too once walked in darkness as you do now, Stefan.


Oh no - I'm using ArrayList today and since one or two years, and suggest any newbie to use it too.

But it took some time (and usage of threads) to realize the advantages.

Explaining misbehaviour needn't be excuse of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic