When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.
In Java, each object is associated with a lock. The term lock refers to the access granted to a particular thread that has entered a synchronized method. Monitor refers to a portion of the code in a program. It contains one specific region (related to data or some resource) that can be occupied by only one thread at a time. This specific region within a monitor is known as critical section. A thread has exclusive lock from the time it enters the critical section to the time it leaves. That is, the data (or resource) is exclusively served for the thread, and any other thread has to wait to access that data (or resource). Some common terminology while using monitors is as follows: entering the critical section is known as acquiring the monitor, working with the critical section (data or resource) is known as owning the monitor and leaving the critical section is known as releasing the monitor. [Read more…] about Thread Synchronization in Java