aspose file tools*
The moose likes Java in General and the fly likes Will storing a HashMap inside a Hashtable make the HashMap threadsafe? 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 "Will storing a HashMap inside a Hashtable make the HashMap threadsafe?" Watch "Will storing a HashMap inside a Hashtable make the HashMap threadsafe?" New topic
Author

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

Ernesto Layug
Greenhorn

Joined: Oct 03, 2009
Posts: 3

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernesto Layug
Greenhorn

Joined: Oct 03, 2009
Posts: 3
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

Joined: Oct 03, 2009
Posts: 32


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);


SCJP ,SCWCD
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Oct 03, 2009
Posts: 3

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will storing a HashMap inside a Hashtable make the HashMap threadsafe?