IT Questions and Answers :)

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:

Related Posts:

0 comments:

Post a Comment

 On 30 september 2021, at 20:01, Dominick commented on which of following is not cloud
 On 20 july 2021, at 11:50, Anonymous commented on which of following is false about ddr2
 On 03 march 2021, at 13:49, Anonymous commented on which of following layers is layer 4 in
 On 02 january 2021, at 20:23, Anonymous commented on when open source software is used in
 On 30 october 2020, at 01:42, Anonymous commented on which of following is true regarding
 On 16 october 2020, at 18:27, Anonymous commented on which of following modules cannot be
 On 15 october 2020, at 13:54, Myles commented on in javascript which of following
 On 24 june 2020, at 05:50, Anonymous commented on in which layer of osi model would you
 On 26 may 2020, at 15:01, Myles commented on in javascript which of following
 On 24 february 2020, at 10:56, Anonymous commented on what is difference between tacacs and
 On 18 february 2020, at 11:56, Anonymous commented on what video conferencing application
 On 20 november 2019, at 18:18, Ranjitkumar commented on irq 1 is commonly assigned to the
 On 20 november 2019, at 12:36, RMS commented on irq 1 is commonly assigned to the
 On 06 september 2019, at 14:40, Ranjitkumar commented on what is acronym for management system
 On 06 september 2019, at 11:07, RMS commented on what is acronym for management system
 On 05 september 2019, at 00:48, Anonymous commented on how do you block user from opening
 On 27 august 2019, at 16:47, Anonymous commented on at what location in microsoft windows
 On 23 july 2019, at 22:31, Anonymous commented on in cryptography initialization vector
 On 17 july 2019, at 10:30, Anonymous commented on which of these is not use case of
 On 28 june 2019, at 06:38, Anonymous commented on which of following is not computer

Popular Posts