6

Initially I thought a volatile variable was better than synchronized keyword as it did not involve BLOCKING or CONTEXT SWITCHING. But reading this I am now confused.

Is volatile implemented in a non-blocking approach using low level atomic locks or no?

Community
  • 1
  • 1
chrisapotek
  • 6,007
  • 14
  • 51
  • 85

5 Answers5

6

Is volatile implemented in a non-blocking approach using low level atomic locks or no?

Volatile's implementation varies between each processor but it is a non-blocking field load/store - it is usually implemented via memory-fences but can can also be managed with cache-coherent protocols.

I just read that post. That poster is actually incorrect in his explanations of Volatile vs Synchronized flow and someone corrected him as a comment. Volatile will not hold a lock, you may read that a volatile store is similar to a synchronized release and a volatile load is similar to a synchronized acquire but that only pertains to memory visibility and not actual implementation details

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • I did not read the comments in that post. That best wrong answer must be edited there. :) Lesson learned: don't take StackOverflow as the final word. – chrisapotek Feb 27 '12 at 17:24
  • @chrisapotek Yea, sometimes its unfortunate the OP is the only one that can answer the question correct or incorrect. – John Vint Feb 27 '12 at 18:39
  • Public pressure caused the dude to switch to the better answer. That said, it's always a good idea to take SO answers in proper context. Thanks john, @chrisapotek. Might want to edit your question to point that out. – Gray Feb 28 '12 at 18:03
6

Is volatile implemented in a non-blocking approach using low level atomic locks or no?

Use of volatile erects a memory barrier around the field in question. This does not cause a thread to be put into the "BLOCKING" state. However when the volatile field is accessed, the program has to flush changes to central memory and update cache memory which takes cycles. It may result in a context switch but doesn't necessary cause one.

Gray
  • 115,027
  • 24
  • 293
  • 354
3

It's true that volatile does not cause blocking.

However, the statement

a volatile variable was better than synchronized keyword as it did not involve BLOCKING or CONTEXT SWITCHING.

is very debatable and depends heavily on what you are trying to do. volatile is not equivalent to a lock and declaring a variable volatile does not give any guarantees regarding the atomicity of operations in which that variable is involved e.g. increment.

What volatile does is prevent the compiler and/or CPU from performing instruction reordering or caching of the specific variable. This is known as a memory fence. This nasty little mechanism is required to ensure that in a multithreaded environment all threads reading a specific variable have an up-to-date view of its value. This is called visibility and is different from atomicity.

Atomicity can only be guaranteed in the general case by the use of locks (synchronized) or atomic primitives.

What can be however, confusing, is the fact that using synchronization mechanisms also generates an implicit memory fence, so declaring a variable volatile if you're only going to read/write it inside synchronized blocks is redundant.

Tudor
  • 61,523
  • 12
  • 102
  • 142
1

Volatile is a java language modifier and how it is providing its guarantees comes down to the JVM implementation. Putting it simple if you set a primitive field as volatile you guarantee whatever thread reads this field it will read the most recent value. It basically prohibits any JVM behind the scenes optimizations and forces all the threads to cross the memory barrier to read the volatile primitive.

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • I am asking about implementation details, what goes on behind the scene, in the JVM default implementation. What it does I already know. :) – chrisapotek Feb 27 '12 at 17:10
  • The JVM comes with a specification and there are plenty of implementations out there: http://en.wikipedia.org/wiki/List_of_Java_virtual_machines. You can ie. look at the C code of the Hotspot JVM but do you really want to do that? Aren't the guarantees enough for you? Same goes for any native methods in the jdk library I never looked at the c code on my installed Hotspot JVM but I know what it can/cannot do for me that is most of the times very well documented. – dimitrisli Feb 27 '12 at 17:13
0

BLOCKING means that threads don't wait for each other when reading the same volatile variable doing it without mutual exclusion. However, they trigger putting fences on the hardware level to observe "happens-before" semantics(no memory reordering).

To make this more clear, volatile variable is non-blocking because whenever it is read/ by multiple threads concurrently, CPU-cores tied to their threads communicate directly with the main memory or via CPU cache-coherency (depends on hardware/JVM implementation) and no locking mechanism is put in place.

CONTEXT-SWITCHING The volatile keyword does not trigger context switching itself from its semantics, but possible and depends on lower-level implementations.

Ivan Voroshilin
  • 5,233
  • 3
  • 32
  • 61