When a resource is being shared by more than one thread, synchronization is needed which can be done by the keyword synchronized in java.
Take for example, a thread made a change to the shared data and another thread wants to now access this data, the data that his resource will get should contain all the changes made by the previous thread.To avoid anonymity and confusion coordination of events it is needed so that the threads are in unison.
// Only one thread can execute at a time.
// sync_object is a reference to an object
// whose lock associates with the monitor.
// The code is said to be synchronized on
// the monitor object
synchronized(sync_object)
{
// Access shared variables and other
// shared resources
}