Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap access problem

 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic