Its one thing I don't get about concurrency - threads and atomic-actions. According to docs.oracle these actions are specified as atomic:
But at the same time docs.oracle asserts that incrementing a variable is not an atomic action. I thought that this was a write:
private int number;
number++;
Obviously I do not understand what is meant with "reads" and "writes".
Could someone explain this and give an example of "reads" and "writes"?
Edit: When doing this without synchronization the programs suffers from ´thread interference. SO I understand that its not atomic. But when I change another variable that belongs to an object that these threads share - there are NO interference whatsoever. The shared variable from this object is changed via a mutator.
In order to implement number++, the runtime needs to
Acquire the current value of number (a read).
Increment that value.
Write that new value back to number (a write).
If another thread starts from 1 when you're at 2 then the final value for number will be incorrect since that thread will still be reading the original value.
If 1, 2 and 3 are executed as an atomic operation (i.e. a thread cannot start 1 until you've finished 3) then all will be well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With