aspose file tools*
The moose likes Threads and Synchronization and the fly likes volatile variable in multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "volatile variable in multithreading" Watch "volatile variable in multithreading" New topic
Author

volatile variable in multithreading

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi,


Volatile Variable is stored in main memory so that all thread can access the updated volatile variable.what is this main memory exactly in JVM ?How it stores volatile variable?
Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
Volatile fields are instance or class (static) variables and are stored in the heap.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sharmistha Sarkar wrote:Volatile fields are instance or class (static) variables and are stored in the heap.


All member variables are stored on the heap. Nothing special about volatile there.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

priyanaka jaiswal wrote: Volatile Variable is stored in main memory so that all thread can access the updated volatile variable.what is this main memory exactly in JVM ?How it stores volatile variable?


The "main memory" that's referred to here is just the RAM in your computer. Each thread may have local copies of shared variables, such as in L1 or L2 cache, so a thread running on one CPU might be dealing with its local on-chip cache copy of a variable, while a thread running on a different CPU might have its own, separate copy of that variable in its on-chip cache.

By marking a variable volatile, we're saying that's not allowed to happen. All access to that variable must be against a single copy--no local caching.

drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
volatile has to do with the visibility issue which means other threads might possibly see an old value of a variable which has been operated by another thread, if it's modified by volatile, there would be no visibility issue on this variable in multithreaded environment.


science belief, great bioscience!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: volatile variable in multithreading