Google News
logo
Java Multithreading - Interview Questions
How to detect a deadlock condition? How can it be avoided?
We can detect the deadlock condition by running the code on cmd and collecting the Thread Dump, and if any deadlock is present in the code, then a message will appear on cmd.
 
Ways to avoid the deadlock condition in Java :
 
Avoid Nested lock : Nested lock is the common reason for deadlock as deadlock occurs when we provide locks to various threads so we should give one lock to only one thread at some particular time.
Avoid unnecessary locks : we must avoid the locks which are not required.
Using thread join : Thread join helps to wait for a thread until another thread doesn't finish its execution so we can avoid deadlock by maximum use of join method.
Advertisement