aspose file tools*
The moose likes Java in General and the fly likes HashMap  access problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap  access problem" Watch "HashMap  access problem" New topic
Author

HashMap access problem

Steve Yu
Ranch Hand

Joined: Mar 26, 2003
Posts: 60
In the following code ---
public class MyHashMap {
private static HashMap myHashMap;
public static synchronized HashMap getMyHashMap() {
if(myHashMap == null) {
.... // create the myHashMap;
}
return myHashMap;
}
}
Now if I have some programs that allows multiple threads to call "MyHashMap.getMyHashMap()" and get the object value using some key, is it thread safe to do that ? (We can assume that we don't do any "put" for the HashMap instance, only reading is needed).
thanks.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think if the whole method is synchronized as you showed you are fine. But read up on why Double Checked Locking Is Broken. This technique was once recommended in Sun tutorials, but is not good. I prefer what the article shows under Making it Work for Static Singletons:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
Hi,
If u r only going to read the HashMap then the method synchronization is enough.
But u must be sure tht none of the threads is calling a put() on the hashmp.
Better idea will be to synchronize the HaspMap instance using the "Collections.getSynchronizedMap" class.
then I suppose you dont even need the method synchronization.
Let me know if tht works for you,
Bye,
Chinmay


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Steve Yu
Ranch Hand

Joined: Mar 26, 2003
Posts: 60
Thanks for your response. I also want to know -- Does this singleton improve any speed or memory ? I think the answer is it saves memory, but when there are multiple threads it does not help on speed, right ? Because when one thread is executing the synchronized "getMyHashMap" method, the other threads have to wait. So, if I create the HashMap for each thread, it may waste some memory but it doesn't hurt speed at all. Is my understand correct ? Which way is faster ?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If you only need a singleton, why create multiple instances.
Once your singleton has been created and loaded (both best done in a thread-safe manner), then subsequent read-only accesses by multiple threads do not require synchronization. Note that synchronization comes at a cost, so only use it if needed.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
As others have noted, if you're only doing reads, synchronization will not be an issue. If you're doing write, the Javadocs explicitly warn, "Note that this implementation is not synchronized".
Let's suppose for the moment that you're doign reads and writes, then your object is not thread safe. The getMyHashMap() is thread safe, but once mutliple threads have references to the instance, they can modify the state of the instance arbitrarily, that is

may throw an exception. To avoid this you need to do the following

--Mark
 
wood burning stoves
 
subject: HashMap access problem