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

Discussion

Which of these statements about whether volatile makes an increment atomic is correct?

  • A.volatile makes count++ atomic.
  • B.volatile makes count++ atomic only when a single thread writes to it.
  • C.volatile makes count++ atomic provided the variable is also final.
  • D.volatile does not make count++ atomic.

Answer: D

An increment expression is not an atomic action: it reads the value, adds one and writes it back, and two threads can interleave those steps and lose an update. volatile makes each individual read and write atomic and visible, which does not help across three of them. Compound updates need synchronization or a class from java.util.concurrent.atomic.

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

Post your comment

Please log in or register to post a comment.