A deadlock consists of a set of blocked processes, in which each process is holding a resource and waiting to receive another resource which is already owned by another process. It may occur when two processes/threads want the same resource, and there comes confusion about which process should take the resource first and then deadlock occurs. We use the synchronized keyword before the class name and method name to avoid deadlock.

Deadlock in Java

Java deadlock describes the situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in a different order.

See the following figure.

Deadlock in Java

In the above diagram, thread1 requires the resource/object which is held by thread2 and thread2 requires the object which is owned by thread3 and so on. As thread2 does not release its resource and thread1 waiting for a long time, there comes deadlock.

#java #deadlock #java deadlock

Deadlock in Java Example | Java Deadlock Tutorial
2.25 GEEK