Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will storing a HashMap inside a Hashtable make the HashMap threadsafe?

 
Ernesto Layug
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everyone, a newbie here.

My general question would be: if I store a non-synchronized object inside a synchronized object, will the non-sychronized object be threadsafe already?

The actual case is that we are running a web application that makes use of HashMap as the primary container for most of the data. Hashmaps as we know are not threadsafe. As a workaround, we added code to store the HashMap into a Hashtable , then we will store the Hashtable into the session.

Does this approach make the HashMap threadsafe? Thanks a lot. Any input would be appreciated.


 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you check out the ImmutableHashMap (perhaps its just ImmutableMap) in the Google collections suite. They are thread safe and fast. The design was driven to be easy to write and read, and safe

See
google-collections/
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernesto Layug wrote:
Does this approach make the HashMap threadsafe? Thanks a lot. Any input would be appreciated.


First, it depends on what you do with the collection. There are cases where using a hashtable isn't threadsafe. All a synchronized data structure provides you, is the ability to have calls to the methods be "atomic" -- "thread safe" is something else entirely.

second, it depends on what you mean by "hashmap inside a hashtable". Do you mean the elements that were originally in the hashmap are moved to the hashtable? Or do you mean actually putting the hashmap into the hashtable -- adding a layer of indirection? In the first case, see point one. In the second case, no, that doesn't do anything.

Henry
 
Ernesto Layug
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Thanks Pat and Henry for replying. Wasn't expecting to see some replies so quickly.

To Pat: Actually my team leader is not inclined to take non-core Java packages as of now, so we will be stuck with these collections meantime. Thanks anyway for the recommendation. Appreciated.

To Henry: Sorry if my question was a bit ambiguous. A short background on this inquiry could help clarify things. We have a simple Struts-based Java web application and inside our Java code, we are putting the user data into a Hashmap, then into a Hashtable, before setting the Hashtable into the session.

Our regular flow:


Does this approach run the risk of shared data between two concurrent users? I mean because we are using a non-synchronized object as our primary container, 2 sessions could end up mixing each others data? Thanks so much for taking time to respond to me.
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Only one thread can modify a hash table at one point of time.it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released

It means that hm1 object will be threadsafe. It won't matter how the object was created


you can make Hashmap as synchronize :
Map m = Collections.synchronizeMap(hashMap);

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernesto Layug wrote:WActually my team leader is not inclined to take non-core Java packages as of now, so we will be stuck with these collections meantime. .


I understand. You may want to look at them anyway. Their design is so good that there is talk of including them in Java 7. Google is using them very heavily internally.

I really like them a lot.
 
Mike Simmons
Ranch Hand
Posts: 3078
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiaamaa Liammes wrote:It means that hm1 object will be threadsafe. It won't matter how the object was created

No, it won't. Two different threads can still access the map concurrently. Only acquiring the initial reference from the hashtable is thread-safe. Once multiple threads have acquired the reference though, nothing prevents them from doing non-thread-safe things with those references.

Ernesto: if you can't convince your team lead, you can do something similar with the standard library:

The key here is, do not let the "tempMap" reference be used by any other code once the unmodifiableMap() has been created. Other code should only have a reference to the unmodifiable version of the map.

But really, you need to poke your team lead with a stick until they let you use Google Collections.

Pat: yeah, I was checking out the rest of the Guava project as well. Looks very promising. You'd almost think that Java is capable of moving forward. ;)
 
Ernesto Layug
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the helpful replies everyone. Now at least I have an idea where I am. Let me try and talk my team lead into implementing the suggestions here.

Javaranch/Coderanch rocks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic