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());
}
}