Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. Consider a scenario when two thread (Thread1 and Thread2) are accessing same variable 'mObject' with value 1.
  2.  
  3. 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.
  4.  
  5. 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
  6.  
  7. private volatile int mObject;
  8.  
  9. in this scenarion the Thread1 will be getting updated value of mObject
Add Comment
Please, Sign In to add comment