Suppose a thread acquires reentrant lock on a resource multiple times and dies. What will happen to the resource? Can another client access the resource?
Asked
Active
Viewed 335 times
-1
-
Can you show your example code which you used to test this out and clarify to us what is causing the confusion? – Jeroen Vannevel Oct 10 '14 at 13:42
-
Out of curiosity, I asked this doubt. Since re-entrant locks in java maintain holdCount and releases lock once holdCount goes to zero. – a3.14_Infinity Oct 10 '14 at 13:43
-
2It's easily testable: `public void run() { sharedLock.lock(); return; }` – Victor Sorokin Oct 10 '14 at 13:44
2 Answers
3
Proof by demonstration:
public static void main(String[] args) throws InterruptedException {
final ReentrantLock lock = new ReentrantLock();
final Thread t = new Thread(()->lock.lock());
t.start();
t.join();
System.out.println(t.isAlive());
System.out.println(lock.tryLock());
}
It prints
false
false

Marko Topolnik
- 195,646
- 29
- 319
- 436
0
Sounds like a classical deadlock to me. If the lock is not released by the owner - it will stay locked for ever and for everyone.

light_303
- 2,101
- 2
- 18
- 35