Java Thread How to - Make sure a program to go into deadlock








Question

We would like to know how to make sure a program to go into deadlock.

Answer

public class Main {
  public static void main(String[] args) {
    Student a = new Student("A");
    Student b = new Student("B");
    new Thread(()->a.bow(b)).start();
    new Thread(()->b.bow(a)).start();
  }//  ww w . j av  a 2  s .com
}
class Student {
  String name;
  public Student(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }
  public synchronized void bow(Student bower) {
    System.out.format("%s: %s" + " is waiting for me!%n", this.name,
        bower.getName());
    bower.bowBack(this);
  }
  public synchronized void bowBack(Student bower) {
    System.out.format("%s: %s" + " is waiting for me!%n", this.name,
        bower.getName());
  }
}