More details are provided below about the lock acquisition policy implemented by the methods mentioned above. These methods are defined by the Lock interface and implemented by the ReentrantLock class.

void lock()

If the lock is not held by another thread, the lock hold count is set to 1 and the method returns immediately.

If the current thread already holds the lock, the lock hold count is incremented by 1 and the method returns immediately.

If the lock is held by another thread, the thread waits until the lock is acquired, at which point the lock hold count is set to 1.

void unlock()

If the current thread is the holder of this lock, the lock hold count is decremented. If the lock hold count now has the value 0, the lock is released.

If the current thread is not the holder of this lock, then an IllegalMonitorState-Exception is thrown.

Note that a lock action on a lock must be matched by an unlock action.

The thread must make sure that an acquired lock is released after use, typically in the finally block of a try-finally statement.

boolean tryLock()

If the lock is not held by another thread, the lock hold count is set to 1 and the method returns true. This method does not honor fairness—it does not care if there are any threads already waiting for this lock.

If the current thread already holds the lock, the lock hold count is incremented by 1 and the method returns true.

If the lock is held by another thread, the method returns immediately with the value false.

Click here to view code image

boolean tryLock(long time, TimeUnit unit) throws InterruptedException

Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted. In this case, the lock hold count is set to 1 and the value true is returned. The method respects the fair ordering policy if one has been specified for this lock.

If the current thread already holds the lock, the lock hold count is incremented by 1 and the method returns true.

If the current thread is interrupted while waiting to acquire the lock, an InterruptedException is thrown.

If timed out, the method returns the value false.

Click here to view code image

void lockInterruptibly() throws InterruptedException

If the lock is not held by another thread and the current thread is not interrupted, the lock hold count is set to 1 and the method returns immediately.

If the current thread already holds the lock, the lock hold count is incremented by 1 and the method returns immediately.

If the lock is held by another thread, the current thread waits for the lock. If the lock is acquired by the current thread while waiting, the lock hold count is set to 1.

If the current thread is interrupted while waiting to acquire the lock, an InterruptedException is thrown.

The following selected methods for querying a lock are only defined by the ReentrantLock class:

boolean isFair()

Returns true if this lock has the fairness policy set to true.

boolean isLocked()

If the lock is held by any thread, this method returns true; otherwise, it returns false.

int getQueueLength()

Returns an estimate of the number of threads waiting to acquire this lock.

boolean hasQueuedThreads()

Returns true if any threads are waiting to acquire this lock; otherwise, it returns false.

Click here to view code image

boolean hasQueuedThread(Thread thread)

Returns true if the given thread is waiting to acquire this lock; otherwise, it returns false.