IT Questions and Answers :)

animated-update-image-0026

COVID-19 India HEAT MAP

A message to our readers about COVID-19

With the uncertainty surrounding the outbreak of the corona virus. We want to reassure our reader stay safe and Healthy.Be aware and alert. Work as one nation. Sanitize and disinfect all equipment before entering your home, work and anywhere.

Monday, February 26, 2018

What happens when a volatile variable is declared in Java programming?

What happens when a volatile variable is declared in Java programming?

  • All reads and writes go straight to main memory
  • It holds a lock
  • It behaves as if enclosed in an asynchronous block
  • The value of the variable will be cached locally 

 
What happens when a volatile variable is declared in Java programming?

EXPLANATION

The Java volatile variable is an example of a special mechanism to guarantee that communication happens between threads. Basically, the value of the variable can be modified by different threads. When you declare a volatile variable, the value of this variable will never be cached thread-locally. Instead, all reads and writes will go straight to main memory. Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.
Access to a volatile variable never has the potential to block because you can only ever do a simple read or write, so unlike a synchronized block it never holds on to any lock. Because of this, volatile variables are not suited for cases where you want to use read-update-write as an atomic operation, unless you are willing to miss an update. 

Share:

0 comments:

Post a Comment

Popular Posts