Answer:
There are many ways which listed below.
Step-by-step explanation:
lock(): Acquires the lock. If the lock is not available, the calling thread will be blocked until the lock is available.
lockInterruptibly(): Acquires the lock unless the thread is interrupted. If the thread is interrupted before the lock is acquired, an InterruptedException will be thrown.
tryLock(): Tries to acquire the lock. If the lock is available, it is acquired and true is returned. If the lock is not available, the method returns false immediately.
tryLock(long time, TimeUnit unit): Tries to acquire the lock within the given time. If the lock is acquired before the timeout, true is returned. If the timeout expires before the lock is acquired, false is returned.
unlock(): Releases the lock.
newCondition(): Returns a new Condition instance that is bound to this lock. The Condition can be used to implement complex waiting and signaling protocols.