Skip to content

Commit 5143320

Browse files
committed
feat(concurrency): demonstrate reentrant behavior using ReentrantLock with nested method calls
WHAT: - Added `ReentrantExample2` class using a `ReentrantLock` to showcase **reentrancy**. - Implemented `outerMethod()` which locks, prints, and calls `innerMethod()`. - Implemented `innerMethod()` which locks again, proving that the same thread can acquire the lock multiple times. - Main creates an instance and calls `outerMethod()` to demonstrate the flow. WHY: - In real systems, methods often call other synchronized methods on the same object. - Without **reentrancy**, such nested calls would deadlock because the thread already holds the lock. - Reentrant locks maintain a **hold count** → the same thread can re-acquire the lock safely, but must unlock the same number of times. HOW IT WORKS: 1. Thread acquires lock in `outerMethod()`. 2. Calls `innerMethod()`, which **again acquires the same lock**. 3. ReentrantLock allows it since the caller thread already owns the lock. 4. Both `outerMethod` and `innerMethod` print messages. 5. Locks are released in LIFO order via `finally` blocks. OUTPUT (example): Outer Method Inner Method BENEFITS & USE CASES: - ✅ Prevents deadlocks when methods call each other recursively. - ✅ Allows fine-grained locking with advanced features (`tryLock`, `lockInterruptibly`). - ✅ Useful in libraries, frameworks, and systems where nested monitor locks are common. KEY TAKEAWAYS (Interview-Ready): - `ReentrantLock` works like `synchronized` but with more control (timeouts, fairness). - Same thread can lock multiple times → must unlock equal times. - Always release locks in `finally` to avoid resource leaks. - Example: in a banking system, `withdraw()` might internally call `checkBalance()` → both can safely share the same lock. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 534b6d1 commit 5143320

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Section19MultiThreading/Locks/src/Lock/ReentrantExample.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import java.util.concurrent.locks.Lock;
22
import java.util.concurrent.locks.ReentrantLock;
33

4-
static class ReentrantExample {
5-
private final Lock lock = new ReentrantLock(); //Can enter same methods again. enter it again.
6-
//One lock hai.
7-
//Ek count maintain keya jata hai kitni baar acquire keya Guya hai.
4+
static class ReentrantExample2 {
5+
private final Lock lock = new ReentrantLock();
6+
/* Can enter the same methods again. enter it again.
7+
One lock hai.
8+
Ek count maintain keya jata hai kitni baar acquire keya Guya hai.*/
89

910
public void outermethod() {
1011
lock.lock();
@@ -17,7 +18,7 @@ public void outermethod() {
1718
}
1819

1920
//Both methods try to finish the each others its is called deadlock.
20-
//Outer method depends on inner method and inner method depends outer methods to get finish.
21+
//Outer method depends on inner method, and inner method depends on outer methods to get finish.
2122

2223
public void innerMethod() {
2324
lock.lock();
@@ -30,9 +31,12 @@ public void innerMethod() {
3031
}
3132

3233
public static void main(String[] args) {
33-
ReentrantExample R = new ReentrantExample();
34+
ReentrantExample2 R = new ReentrantExample2();
3435
R.outermethod();
3536
}
3637

37-
//Reentrant lock:
38-
// 1. Lock 2.Unlock 3.Try/Lock types: without time and with time. 4. DeadLock(Reentrant lock). 5.lock.Interruptibly();
38+
/*
39+
Reentrant lock:
40+
41+
1. Lock 2.Unlock 3.Try/Lock types: without time and with time. 4. DeadLock(Reentrant lock). 5.lock.Interruptibly();
42+
*/

0 commit comments

Comments
 (0)