Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Consider a scenario when two thread (Thread1 and Thread2) are accessing same variable 'mObject' with value 1.
- when a Thread1 runs, it doesn't expect other threads to modify the variable 'mObject'. In this scenario the Thread1 caches the variable 'mObject' with value 1.
- And if the Thread2 modify the value of 'mObject' to 2, still the Thread1 would be refering the mObject value as 1 since it did caching. To avoid this caching we should to declare the variable as
- private volatile int mObject;
- in this scenarion the Thread1 will be getting updated value of mObject
Add Comment
Please, Sign In to add comment