| Author |
Using HashMap vs Hastable
|
Haris Karameh
Ranch Hand
Joined: Oct 17, 2002
Posts: 38
|
|
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
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Originally posted by Haris Karameh: Is HashMap safe for multiple thread read?
Yes, if it's not being changed at all after init().
|
[Jess in Action][AskingGoodQuestions]
|
 |
Mingwei Jiang
Ranch Hand
Joined: Feb 19, 2004
Posts: 63
|
|
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.
|
 |
 |
|
|
subject: Using HashMap vs Hastable
|
|
|