I have two threads and one class.
Thread1 updates the local object of the class via 3 different methods.
The local object (behind the scene) uses some sort of ArrayList to store data.
Three methods (mentioned earlier), are doing something with elements of the list…
Thread2 queries the local object (content of array list).
PROBLEM:
How can I make sure that none of the three updating methods (Thread1) will modify the content of the “local object” while it’s being read by Thread2?
Thanks
Put a common lock around portions of the code that modify the ArrayList using synchronized. It doesn’t really matter what object you use as a lock, as long as it’s the same, but I think using the ArrayList itself would be idiomatic: