File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Access HashMap by multiple threads concurrently Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Access HashMap by multiple threads concurrently" Watch "Access HashMap by multiple threads concurrently" New topic

Access HashMap by multiple threads concurrently

naras reddy

Joined: Dec 10, 2010
Posts: 2
hi All,
I have a static HashMap will be shared by multiple threads in a real time applicaiton. My requirement is for every thead being created in the applicaiton works with one single key/value pair in its lifetime.

Need help im my below understanding:
I understand for every thread being created works with one sinle key/value pair and will have seperate memory space for the variables created inside method(seperate copy of list variable will be maintained for each thread in process method).

1. Do i need to synchronize list variable inside process method?
2. Please clarify whether i should synchronize process method in my below example?
3. How is shoud handle these kind of requirements in my realtime applications?

thanks in advance.

Below is same example code program.

Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

naras reddy wrote:hi All,
I have a static HashMap

No you don't. Objects do not have the property of being static or non-static. Possibly you mean that you have a static HashMap variable.

1. Do i need to synchronize list variable inside process method?

If you are first populating that map and from then on only reading it, and never changing it, then you don't need to synchronize for mutual exclusion. However, without syncing there's still a possibility that not all the populating thread's writes will be seen by reads from other threads. You're best of to just synchronize all access to the map.

If the above is not your model of operation--that is, if one thread can be writing while another is either reading or writing--then all access to the map must be synchronized. You can do this yourself with explicit synchronization, or you can use Collections.synchronizedMap(myHashMap). A better approach, however, would be to just use java.util.concurrent.ConcurrentHashMap.

Note that, no matter which solution you choose, you may still need to do some explicit synchronization, depending on how you use the map and what your specific requirements are.
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

From your description and example it appears to me you're actually looking for a ThreadLocal class (see its Javadoc). You could use that without any synchronization whatsoever in your code, this class already cares for all sync that is needed.
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Looking at your description, I agree with Martin that ThreadLocal may be the way to go.

However, looking at your code, something doesn't jive:

For this to work the way you describe, whenever you call process(), you'll have to pass it the name of the current thread (or at least some String that you make sure is unique and consistent per thread). If that's not what you're doing, then it's not the "one entry per thread" scenario that you described. And if that is what you're doing, well, you can make it work that way, but it's rather fragile.

A better approach would be to not pass "name" in as a parameter, but just set it to the current thread's name in right before accessing the map. And then, if you're doing that, you're basically replicating what ThreadLocal does for you, so you may as well skip the map altogether and just use a ThreadLocal<List<Integer>>.
I agree. Here's the link:
subject: Access HashMap by multiple threads concurrently
It's not a secret anymore!