2

I am a little confuse about how volatile variable effectively accesses from "main" memory. How's it different from a variable (non-volatile) that has a local copy ? What's the typical workflow whenever multiple threads accesses a non-volatile vs a volatile variable ? I mean how do they work behind the scene ?

4
  • possible duplicate of Volatile keyword in Java - Clarification Commented Aug 4, 2012 at 15:15
  • I wasn't that clear after reading that thread, so I opened this question. i am actually asking more about the workflow and memory access instead of the concept itself Commented Aug 4, 2012 at 15:20
  • 1
    Sorry for that offtop but I think it is worth to mention that one of advantage of volatile is that it also ensure atomic reading of primitives. It is useful in case of 64bits variables (like long or double) on 32 bits processors machines because they prevent updating variable between reading first 32 and last 32 bits of variable. Commented Aug 4, 2012 at 15:43
  • Thanks for the notes. 64-bit numeric variables (double and long) that are not declared volatile. Commented Aug 4, 2012 at 15:46

1 Answer 1

5

Let's say you have a variable that can be accessed by multiple threads.

Thread 1 looks at the variable. Because looking at shared memory is more expensive than thread-local memory, it makes a copy of the variable. (Note that an object won't be copied, just its reference.)

Thread 2 looks at the same variable. It decides to change the variable. But Thread 1 doesn't know it! Thread 1 is still using stale data. This is a Very Bad Thing. By making it volatile, each thread must look at the original variable when accessing it. They aren't permitted to make local copies, so it won't get stale.

Sign up to request clarification or add additional context in comments.

8 Comments

by saying shared memory you mean Heap ? and thread-local memory means the Stack on each Thread ? and where does that copy of variable go ?
where it goes physically is up to the implementation of the JVM. We don't know or care where it goes. All we know is that threads are allowed to cache variables, but if they're volatile, they can't.
Ok what if I use Lock instead of Volatile ? is it going to be like thread make a copy of the variable on its local stack, and it updates that variable (mutable) to the shared memory once it exits the lock, so that the next thread will always see the most updated value ?
if you use a lock instead of a volatile, then you can guarantee that no one else overwrites your value while you're using it. For example, if in a given method you had to use its value four times, if halfway through the method some other thread changes your variable, that's bad. But if you don't need to use it in that fashion, then a lock is overkill.
Correct I understand the concept. I just want to clarify and confirm about how does each thread set the value back to the shared memory once it exits the lock so that the next incoming thread will see that latest value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.