java - Atomic actions - what is meant by reads and writes? -
its 1 thing don't concurrency - threads , atomic-actions. according docs.oracle these actions specified atomic:
- reads , writes atomic reference variables , primitive variables (all types except long , double).
- reads , writes atomic variables declared volatile (including long , double variables).
but @ same time docs.oracle asserts incrementing variable not atomic action. thought write:
private int number; number++;
obviously not understand meant "reads" , "writes".
could explain , give example of "reads" , "writes"?
edit: when doing without synchronization programs suffers ´thread interference. understand not atomic. when change variable belongs object these threads share - there no interference whatsoever. shared variable object changed via mutator.
in order implement number++
, runtime needs to
acquire current value of
number
(a read).increment value.
write new value
number
(a write).
if thread starts 1 when you're @ 2 final value number
incorrect since thread still reading original value.
if 1, 2 , 3 executed atomic operation (i.e. thread cannot start 1 until you've finished 3) well.
Comments
Post a Comment