Machine-translated by Google. Questions are authored in English.
Top

Discussion

Declaring a long volatile - which of the following is true?

  • A.Declaring a long volatile improves visibility but does not make its reads and writes atomic.
  • B.Declaring a long volatile has no effect.
  • C.Declaring a long volatile makes its reads and writes atomic and its changes visible to other threads.
  • D.Declaring a long volatile makes its reads and writes atomic but says nothing about visibility.

Answer: C

volatile does both jobs: reads and writes become atomic for all volatile variables including long and double, and each write establishes a happens-before relationship with subsequent reads of the same variable, so the change is always visible. A reader also sees the side effects of the code that led up to that write.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.