aspose file tools
The moose likes Threads and Synchronization and the fly likes Using HashMap vs Hastable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Using HashMap vs Hastable" Watch "Using HashMap vs Hastable" New topic
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
    
  15

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Using HashMap vs Hastable
 
Similar Threads
Hashtable vs. HashMap
thread-safe
HashMap Vs Hash table
how to share a hashtable for read only purpose between several threads ?
HashMap or HashTable