我正在查看 Oracle 文档以解决死锁问题。 我找到了这段代码
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();
}
}
我不明白,什么情况下会发生死锁?
我运行了这段代码,它工作得很好。那么一定有一些特殊事件,什么时候会发生死锁?
假设首先在alphonse
对象上调用bow,那么当bower.bowBack(this)
被调用时,它是否会保留对alphonse
对象的锁定在凉亭对象上?
因为如果它保留锁,另一个对象上的 bow
函数将不会获得锁,直到 alphonse
离开它的锁,并且永远不会出现死锁情况。
最佳答案
如果在打印第一行之后、调用 BowBack 之前放置 Thread.sleep(1000),您应该会看到死锁。这种僵局无论如何都可能发生,而且很少见。
您有两个线程,并且以不同的顺序获取两个锁。这可能会使每个线程持有一个锁,但无法获得第二个锁。即陷入僵局。
注意:线程需要很长时间才能启动,这意味着第一个线程可以在第二个线程启动之前运行完成,因此您不太可能会看到该问题。
<小时/>这是给你的一个谜题。这会造成僵局,你能明白为什么吗?
class A {
static final int i;
static {
i = 128;
Thread t = new Thread() {
public void run() {
System.out.println("i=" + i);
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
关于java - 这段java代码是如何产生死锁的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24653620/