Locks are one of the basic synchronization mechanisms provided by the Java concurrency API. It allows the programmers to protect a critical section of code, so only one thread can execute that block of code at a time. It provides the following two operations:
lock(): You call this operation when you want to access a critical section. If there is another thread running that critical section, other threads are blocked until they're woken up by the lock to get the access to the critical section.
unlock(): You call this operation at the end of the critical section, to allow other threads to access the critical section.
In the Java Concurrency API, locks are declared in the
Lock interface and implemented in some classes, ...