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("%s: %s has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s has bowed back to me!%n",
this.name, bower.getName());
}
}
public static void main(String[] args) {
final Friend alphonse = new Friend("Alphonse");
final Friend gaston = new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
我没有得到的是堵塞如何发生。main函数启动两个线程,每个线程都开始各自的弓箭操作。
“同步”到底阻止了什么?为同一对象运行的功能相同(就像我最初想的那样)?同一类的所有对象具有相同的功能?同一对象的所有同步功能?同一类所有对象的所有同步功能?
帮帮我
子衿沉夜
慕婉清6462132
倚天杖
相关分类