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 ?
1
There are 1 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in VOLATILE
- Volatile properties in Kotlin?
- Use of volatile variable in Java
- Is processor cache flushed during context switch in multicore?
- Will mutations of a volatile variable be visible to all threads?
- Should I use 'Volatile' in following case?
- What happen when I run MemoryBarrier() exactly ? and how do it?
- Optimizing multithreaded queue processing code in Java
- Return a value from a Task
- Is simple getter call on volatile variable atomic operation?
- Using memcpy and friends with memory-mapped I/O
- InterlockedExchange issue with C28112/3
- Is it safe to share a volatile variable between the main program and an ISR in C?
- Are variables used by System.Threading.Timer subject to caching?
- Why std::numeric_limits::is_integer is false for volatile types?
- Run runnable by thread pool, should I use volatile?
Related Questions in NON-VOLATILE
- When not to use volatile?
- Linux high-speed(/no impact) storage (for settings) and PHP
- Under what conditions will writes to non-volatile variables be unseen by other threads? Can I force such conditions for experimental purposes?
- How to prevent "partial write" data corruption during power loss?
- Excel replace Indirect function with a non volatile formula but referencing a cell with named ranges in
- Excel - Dynamic ranges without volatile formulas or VBA
- Read/Write from ATtiny1616 EEPROM
- struct Non_const, non_volatile static or external variable
- Java : Volatile variable access
- c# .net in-memory persistence
- non-volatile variables
- Immediate storage of combined DTC - Dem AUTOSAR
- Dereferencing a NULL pointer in embedded
- Does JVM guarantee to cache not volatile variable?
- Do Android ART and HotSpot behave differently in non-volatile variable visibility?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
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.