• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized member access

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there are three objects: an object with a single int field, a thread that increments said field, and a thread that decrements said field. Do both threads need to synchronized on the object containing the int in order for this program to be thread safe? What if only one thread synchronizes on the int field when modifying it?

 
Salil Wadnerkar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All threads who use this field need to be synchronized - otherwise some thread will modify this field while some other thread is using it. By the way, for fields of simple types, consider using atomic types - like AtomicInteger
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about thinking in terms of 'object responsibilities'?

The first object's responsibility is to make sure its value is always correct, no matter how many threads try to increment/decrement its value.
The second object's responsibility is to increment a number
The third objects's responsibility is to decrement a number
The fourth object's responsibility may be to multiply...and so on...

So, if we go with 'single responsibility principle', I can go with a class say Number which contain the int value. I will provide methods such as increment(), decrement(), multiply() in this Number class and make them synchronized just to make sure about the integrity of the int field.

I will have another class NumberIncrementer which will have a non synchronized method increment(). Similarly, I can have another class NumberDecrementer which will have non synchronized method decrement()...and so on...These classes NumberIncrementer,NumberDecrementer will share a common Number object.

This way, I will have to synchronize only in one class and still will be able to protect its integrity.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amar Saikia wrote:. . , I can go with a class say Number which contain the int value. . . ..
Since there already is a class called Number, you should consider a different name.
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Amar Saikia wrote:. . , I can go with a class say Number which contain the int value. . . ..
Since there already is a class called Number, you should consider a different name.


Thanks for your suggestion. Since I don't know the context, I could not able to come up with any meaningful name. However, I agree, that could not be an excuse for suggesting a wrong name.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!