wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Hash Table   -  Sychronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Hash Table   -  Sychronization" Watch "Hash Table   -  Sychronization" New topic
Author

Hash Table - Sychronization

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
Why Hash Table is synchronized ?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Threads and Synchronization forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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 .


http://home.arcor.de/hirnstrom/bewerbung
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 28, 2003
Posts: 11476
    
  94

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hash Table - Sychronization