![deadlock java deadlock java](https://image.slideserve.com/1329055/possible-deadlocks-l.jpg)
On the other hand, thread B also tries to acquire a lock on Object-A, while thread A is acquired a lock on Object-A. Assume that thread A executing method A and wants to acquire the lock on Object-B, while thread B is already acquired a lock on Object-B. The thread A and B acquired the lock of Object-A and Object-B, respectively. It is possible that another thread has already acquired that lock then the thread (that wants to acquire the lock) will have to wait until the previous thread does not release the lock. Nevertheless, if a thread wants to execute a synchronized method it first tries to acquire a lock. It allows that at a time only one thread can access that method. To acquire a lock, Java provides synchronization to lock a method or code block. In this section, we will learn how to detect and avoid deadlock in Java. The question how to avoid deadlock in Java is the most important and popular question asked in a Java interview. The situation arises with more than two threads.
![deadlock java deadlock java](https://image.slideserve.com/1329055/deadlock-resolution-via-exceptions-for-dependable-java-applications-n.jpg)
Since the threads wait for releasing the object, the condition is known as deadlock.
![deadlock java deadlock java](http://3.bp.blogspot.com/-If7Kr1okrlY/Va0Hgw5vi0I/AAAAAAAADcA/irLstYqblDg/s1600/Life%2Bcycle%2Bof%2BThread%2Bin%2BJava%2BStoping%2BPausing%2BRunning.png)
The deadlock is a situation when two or more threads try to access the same object that is acquired by another thread. Sometimes the threads find themselves in the waiting state, forever that is a deadlock situation. The multithreading environment allows us to run multiple threads simultaneously for multitasking. In Java, deadlock is a part of multithreading. Next → ← prev How to Avoid Deadlock in Java