What is a deadlock | Eklavya Online

What is a deadlock

Deadlock is a situation where two or more threads are blocked forever, waiting for each other. This may occur when two threads, each having a lock on one resource, attempt to acquire a lock on the other’s resource. Each thread would wait indefinitely for the other to release the lock, unless one of the user processes is terminated. In terms of Java API, thread deadlock can occur in following conditions:

  • When two threads call Thread.join() on each other.
  • When two threads use nested synchronized blocks to lock two objects and the blocks lock the same objects in different order.