• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Volatile v/s Atomic v/s Synchronized

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have few queries regarding volatile, atomic, synchronized:

1. Can we achieve the same thing using volatile which can be achieved using atomic and vice-versa?

2. Can we achieve the same thing using atomic which can be achieved using syncrhonized and vice-versa?

3. Can we achieve the same thing using volatile which can be achieved using synchronized and vice-versa?

Though about synchronized concept individually i could understand; about volatile -> i think that the variable will never be cached and always be read from main memory. But, in depth details, I get confused a lot.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, by "atomic", I am assuming that you mean the atomic variables ?? ... meaning this ... https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html

Vaibhav Gargs wrote:
1. Can we achieve the same thing using volatile which can be achieved using atomic and vice-versa?



The atomic variables/classes are built using volatile variables (under the hood), meaning that underlying variable within each atomic variable class is volatile. Because of this, I guess you can conclude that atomic variables can do everything that volatile variables can do, but not vice-versa.

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:First, by "atomic", I am assuming that you mean the atomic variables ?? ... meaning this ... https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html



Thank you Henry.

Yes, by Atomic I meant Atomic Variables.

an you please cite some examples for these scenarios to help me understand the exact ones.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:
2. Can we achieve the same thing using atomic which can be achieved using syncrhonized and vice-versa?



Well, yes, and also, no. This needs some explaining, unfortunately. The main feature of atomic variables is adding CAS support to volatile variables. This feature allows code to set a value to a variable conditionally (and supported by the processor).

This may sound like very little, but that is how the synchronize feature works. The OS mutex locks are implemented by the processor CAS instructions, which in turn, provides the locking functionality. The JVM simply uses these locks for synchronize.

The atomic variables brings the CAS support into the JVM. By doing this, you can create lock free implementations. Or it is also called "optimistic locking". The idea is the many threads actually assume that it will not collide and *not* grab a lock. It must work with variables that it doesn't share. Later, when it is completed, it must store the value. This is done with atomic variables. If it is successful, then great -- there was no collision. If not successful, then there was a collision, and the code has to deal with it.

This allows atomic variables to work without locks. And as long as there are no collision, it is incredibly efficient. However, if there are collisions, then it will have to run alternate code, or more commonly, have to redo all the work again. This makes it incredibly inefficient.

Regardless, syncrhonized code, and atomic variables based code, uses completely different algorithms. So, asking whether they can do the same thing may be moot.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:
3. Can we achieve the same thing using volatile which can be achieved using synchronized and vice-versa?



For this third one, of course, the answer is no. This is indirectly explained in my previous posts, but, directly, volatile variables are very simple. Only getting and setting is atomic. And is generally used in that one use case (of getters and setters).

Synchronized can be also be used for getters and setters, and a whole lot more...

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry! Could you please explain about point 3 as well?
3. Can we achieve the same thing using volatile which can be achieved using synchronized and vice-versa?
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Vaibhav Gargs wrote:
3. Can we achieve the same thing using volatile which can be achieved using synchronized and vice-versa?



For this third one, of course, the answer is no. This is indirectly explained in my previous posts, but, directly, volatile variables are very simple. Only getting and setting is atomic. And is generally used in that one use case (of getters and setters).

Synchronized can be also be used for getters and setters, and a whole lot more...

Henry



Thank you Henry!

So, we can achieve everything using synchronized which can be achieved using volatile, correct? I mean the variables updated values are written to main memory as soon as the thread releases the lock of synchronized block?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic