考虑以下:
public class Deadlock {
static class Friend{
private final String name;
public Friend (String name){
this.name = name;
}
public String getName(){
return this.name;
}
public synchronized void bow(Friend bower){
System.out.format("\n%S: %S has bowed to me!" , this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("\n%S: %S has bowed back to me!", this.name, bower.getName());
}
}
public static void main(String[] args) {
final Friend alf = new Friend("Alf");
final Friend arian = new Friend("Arian");
// Thread 1 …Run Code Online (Sandbox Code Playgroud)