我遇到了这个exampleOracle 的 Java 教程描述了多线程场景中的死锁。
因此,在此示例中,我在第 17 行和第 18 行进行了以下更改。
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) {
//My Changes
//System.out.format("%s: %s" + " has bowed to me!%n", this.name, bower.getName()); //Line 17
System.out.println(this.name + ": " + bower.getName() + " has bowed to me!"); //Line 18
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() {
@Override
public void run() {
alphonse.bow(gaston);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
gaston.bow(alphonse);
}
}).start();
}
}
进行这些更改后,程序成功终止,不会导致死锁并打印以下输出
Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed back to me!
Gaston: Alphonse has bowed to me!
Alphonse: Gaston has bowed back to me!
所以我的问题是——为什么它会这样? println语句是如何防止死锁的?
使用与否没有区别System.out.print
or System.out.format
: 他们基本上做同样的事情。
如果执行以下命令,则会出现死锁Gaston.bow(Alphonse)
开始于 开始之间Alphonse.bow(Gaston)
and bower.bowBack(Alphonse)
(反之亦然):两个线程正在等待另一个线程持有的监视器,从而发生死锁。
这种情况的发生不一致,因为它取决于一个微妙的计时问题,取决于线程的调度方式 - 有可能Alphonse.bow
and bower.backBack(Alphonse)
之前完成Gaston.bow
执行完毕,所以看起来没有死锁。
解决这个问题的经典方法是对锁的获取进行排序,以便每次都首先获取第一个相同的锁;这可以防止死锁的可能性:
public void bow(Friend bower) { // Method no longer synchronized.
int firstHash = System.identityHashCode(this);
int secondHash = System.identityHashCode(bower);
Object firstMonitor = firstHash < secondHash ? this : bower;
Object secondMonitor = firstHash < secondHash ? bower : this;
synchronized (firstMonitor) {
synchronized (secondMonitor) {
// Code free (*) of deadlocks, with respect to this and bower at least.
}
}
}
(*) 它不是quite保证无死锁,因为System.identityHashCode
可以为不同的对象返回相同的值;但这不太可能。
这是一个应用程序生日悖论:如果您只有两台显示器,则碰撞的可能性约为 10^-18;但如果您的显示器数量超过 77k,则更有可能发生冲突。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)