i am going through oracle docs for deadlock.. i found this code
public class Deadlock {
static class Friend {
private final String name;
You have 2 objects, alphonse and gaston and 2 threads, Thread1 and Thread2
Suppose this happens:
Thread1: alphonse enters the bow() method. and will hold a lock on the alphonse object
Thread2: gaston enters the bow() method. and will hold a lock on the gaston object
Thread1: alphonse while in the bow() method, calls bowBack() on the gaston object.
-> Thread1 will block, since Thread2 already has the lock on gaston
Thread2: gaston while in the bow() method, calls bowBack() on the alphonse object.
-> Thread2 will block, since Thread1 already has the lock on alphonse
So now Thread1 is waiting for Thread2. And Thread2 is waiting for Thread1. This is a deadlock.