0% found this document useful (0 votes)
11 views3 pages

ReentrantLock Scenarios and Examples

The document explains the usage of ReentrantLock in Java, detailing various locking scenarios such as basic locking, non-blocking attempts with tryLock(), and fair locking. It emphasizes the importance of using try-finally blocks to avoid deadlocks and illustrates reentrancy support where a thread can acquire the same lock multiple times. Additionally, it covers interruptible lock acquisition and the use of timeouts when attempting to acquire locks.

Uploaded by

msrbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

ReentrantLock Scenarios and Examples

The document explains the usage of ReentrantLock in Java, detailing various locking scenarios such as basic locking, non-blocking attempts with tryLock(), and fair locking. It emphasizes the importance of using try-finally blocks to avoid deadlocks and illustrates reentrancy support where a thread can acquire the same lock multiple times. Additionally, it covers interruptible lock acquisition and the use of timeouts when attempting to acquire locks.

Uploaded by

msrbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ReentrantLock in Java – Scenarios and

Examples
1. Basic Locking with ReentrantLock
Use `lock()` and `unlock()` to define a critical section manually.

Example:
ReentrantLock lock = new ReentrantLock();

public void safeMethod() {


lock.lock();
try {
// critical section
} finally {
lock.unlock();
}
}

2. TryLock() – Non-blocking Lock Attempt


Use `tryLock()` to avoid waiting if the lock is not available.

Example:
if (lock.tryLock()) {
try {
// do work
} finally {
lock.unlock();
}
} else {
System.out.println("Lock not available, doing something else...");
}

3. TryLock with Timeout


Try acquiring the lock with a timeout to avoid indefinite waiting.

Example:
try {
if (lock.tryLock(2, TimeUnit.SECONDS)) {
try {
// critical section
} finally {
lock.unlock();
}
} else {
System.out.println("Couldn't acquire lock within 2 seconds");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

4. lockInterruptibly() – Interruptible Lock Acquisition


Allows the thread to be interrupted while waiting for a lock.

Example:
try {
lock.lockInterruptibly();
try {
// critical section
} finally {
lock.unlock();
}
} catch (InterruptedException e) {
System.out.println("Thread was interrupted while waiting for lock.");
}

5. Fair Locking
A fair ReentrantLock gives access to the longest-waiting thread.

Example:
ReentrantLock fairLock = new ReentrantLock(true); // fair policy

// Threads acquire the lock in request order


fairLock.lock();
try {
// critical section
} finally {
fairLock.unlock();
}

6. Reentrancy Support
A thread can acquire the same lock multiple times.

Example:
lock.lock();
lock.lock();
try {
// nested locking allowed
} finally {
lock.unlock();
lock.unlock(); // must unlock the same number of times
}

7. Deadlock Risk if not in try-finally


If unlock() is not in a finally block, exceptions may cause deadlocks.

Bad example:
lock.lock();
doWork(); // might throw
lock.unlock(); // may never be reached!

Correct usage is always:


lock.lock();
try {
doWork();
} finally {
lock.unlock();
}

You might also like