We have to make sure that one thread does not get scheduled and change the data while another thread is in the midst of altering that data. As discussed earlier, the three threads above must synchronize their access to the data being manipulated. At one company I used to work on, there was no version control system, and there were a bunch of source code files. If you wanted to alter a file, you had to walk to a white board, and place a Red Dot on the file you were going to alter. After you are done, you had to erase the red dot to signify that you were no longer editing that file.
Synchronization is very similar conceptually.
You mark an object as being in use, and any other thread wanting to access that object will have to wait till it is released. Synchronization is done by declaring a method or a code block synchronized . If a thread invokes a synchronized method on an object, that object is locked. This means that access to that object is sequential in time. Let s consider an example.