Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

do I need to synchronize or not?  RSS feed

 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashtable implements syncronized this should not cause any problem...
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that he updates the content of the objects that r stored in the hashmap. So using Hashtable does not make any diff.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!