Can someone explain to me how Reentrant lock and deadlock relate to each other with Java code (pseudo) example?
Can someone explain to me how Reentrant lock and deadlock relate to each other
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A reentrant locking mechanism allows the thread holding the lock to re-enter a critical section. This means that you can do something like this:
In a non-reentrant lock, you would have a deadlock situation when you try to call
functionTwo()fromfunctionOne()because the thread would have to wait for the lock…which it holds itself.Deadlock, of course, is the evil situation in which Thread 1 holds lock A and is waiting for lock B while Thread 2 holds lock B and is waiting for lock A. Thus, neither can continue. This code sample creates a deadlock:
The calling thread tries to wait around for the spawned thread, which in turn can’t call
deadlock()until the caller has exited. Ka-boom!