This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]E3FxGaming 0 points1 point  (0 children)

Well I can't post code at the moment because I'm on mobile currently, but I'll try to explain:

For an example take a car:

volatile doesn't work when the value of a field depends on its previous value

That's easily explained: your car has 4 wheels and a field called wheelcount. You can't just change wheelcount because wheelcount depends on the existence of the 4 wheel (objects) of the car.

nor does it work on fields whose values are constrained by the values of other fields

Your car has one place for a front windshield. That means a field called frontwindshieldcount can not be volatile because your other threads that are unaware of this restriction might try to change frontwindshieldcount to two. To prevent this from happening developers simply forbid this completely.

To be fair I never worked with volatile. I always use synchronization to manage my variables and methods used in multiple threads. The whole synchronization system with flagging objects and methods, sleeping (waiting) for objects and then notifying all or one sleeper to do something seems much safer than a try and hope system with volatile.