首页javathreadJava Thread - 如何确保程序进入死锁

Java Thread - 如何确保程序进入死锁

我们想知道如何确保程序进入死锁。
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();
  }
}
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());
  }
}