aspose file tools
The moose likes Threads and Synchronization and the fly likes do I need to synchronize or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "do I need to synchronize or not?" Watch "do I need to synchronize or not?" New topic
Author

do I need to synchronize or not?

Daniil Sosonkin
Ranch Hand

Joined: Jan 15, 2004
Posts: 76
Hi,

I'm having this issue and not really sure what to do. There is this HashMap contianing about 3000 to 120000 elements. Each element is mapped to an object which is never null and contains mostly longs, shorts, and ints inside. I will have one thread updating those objects continuesly and there will be dozens others reading values from those objects. So, the question is, should I synchronize the updated of the values or not?

There is a guarantee that only one thread will update one object, but many will read it or update many different objects.

Help will be appreciated.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
If you do not sync the objects while updating them, then the readers may get broken state of the object.Fro example, if you expect to update 3 ints in the object, the updates will be atimuic, but you dont know when and at what order they will be written back to the main memory. So, the reader may see 2 updated values and 1 old.
vinay prasad
Greenhorn

Joined: Jun 15, 2004
Posts: 8
Hashtable implements syncronized this should not cause any problem...
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
I think that he updates the content of the objects that r stored in the hashmap. So using Hashtable does not make any diff.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: do I need to synchronize or not?
 
Similar Threads
wait and notify method method
NX: doubts on lock/unlock approaches
synchronization
how can i achieve a 100% thread safty method ?
Synchronizing a Hash Map accessed by two threads.