Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using HashMap vs Hastable  RSS feed

 
Haris Karameh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In servlert code there is static Hashtable which is exclusivelly populated in init().
In doGet() the table is read only. Would it be safe to replace Hashtable with HashMap. Is HashMap safe for multiple thread read?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Haris Karameh:
Is HashMap safe for multiple thread read?


Yes, if it's not being changed at all after init().
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if your original Hashtable is read only, it will be safe. But if there're places to put objects in it. You should take care, because HashMap is not synchronized at all. But even Hashtable, at method level, it's synchronized, but at transaction level, it's not, it depends on you. So you should always write code like this when you wanna change the state of Hashs (Whatever it is Hashtable or HashMap):

Suppose you have a Hashtable object named table.

This can guarantee the atomic transaction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!