Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Atomic Variables versus Synchronization

 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use the Atomocity concept as it is more effective than Synchronized block ?
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can as long as you can deal with the complexity of converting a synchronized block to a non synchronized atomic program/block/or anything it means as per your changed algorithm. If you have many variables that need to be atomically updated, you may sometimes need to write your own implementation classes and there are chances that you may need to change your algorithm a great deal. Also you may have to test it thoroughly and testing multi-threaded applications is inherently tough.

I'd consider the readwrite locks' suggestion ( if applicable ) posted above.

Chan.
 
Chan Ag
Rancher
Posts: 1089
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,

Can we use the Atomocity concept as it is more effective than Synchronized block ?


is a very general ( not sure if it is a correct statement) statement. I'm not sure if atomic classes are more effective though ( remember the extra looping - even though it may be just one extra loop ). I think synchronization should be brief and focused and atomic classes should be used if they don't make your implementation very complex and you need to minimize synchronization. I think there is a trade off here and you can't say one is more effective than the other cause they both achieve different things. May be it depends on what you mean by effective.

Of course, there are more qualified people to talk about it. :-)

Chan.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic