2
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();
}
}
在线教程说此代码中发生死锁的位置? Java的
死锁时运行,这是非常有可能的是,当他们尝试调用bowBack两个线程将被阻塞。这两个块都不会结束,因为每个线程都在等待另一个线程退出低头。
但我在这里没有看到任何相互依存关系。任何人都可以解释死锁的位置吗?
报价解释它。 – 2013-04-30 02:59:05
http://stackoverflow.com/questions/749641/trying-to-wrap-my-wee-brain-around-how-threads-deadlock?rq=1 – Thihara 2013-04-30 02:59:56