0

I have read so many articles about the volatile and all says when we use volatile keyword it will read and write the value for that variable in main memory and all volatile variables are stored in a Thread-Local memory over the main memory shared by all threads.I am confused about the main memory, does it mean Heap in terms of java?

Moni
  • 433
  • 3
  • 9
  • Basically, the value will be consistent over all threads. Variables marked not volatile may have different values on different threads, because of things like caching and stuff. – Zizouz212 Feb 26 '16 at 01:37
  • Conceptually, yes, the data is stored in the heap. But getting it from main memory all the time is slow. So the data is cached somewhere and only sync'd back occasionally. Some of those caches (such as machine registers) are visible only to a single thread. Marking a variable volatile makes sure that changes are flushed immediately. – Thilo Feb 26 '16 at 01:40
  • @Nathan Hughes : I think my question is not duplicate because i m not asking the concept of volatile, that answer giving me the concept of volatile but i am more curious to know about the storage. Thilo: Thanks for your response. – Moni Feb 26 '16 at 02:12
  • the linked question answers your question, it describes volatile as indicating that the variable contents can't be cached. that's all there is to it. ThreadLocal is something entirely different. – Nathan Hughes Feb 26 '16 at 02:23

0 Answers0