gpt4 book ai didi

java - 使用 System.out.format 和 System.out.println 进行多线程

转载 作者:行者123 更新时间:2023-12-05 00:44:56 24 4
gpt4 key购买 nike

我遇到了这个 example在 Oracle 的 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没有区别或 System.out.format : 他们基本上都在做同样的事情。

如果执行 Gaston.bow(Alphonse),这里会发生死锁在 Alphonse.bow(Gaston) 的开始之间开始和 bower.bowBack(Alphonse) (反之亦然):两个线程正在等待对方持有的监视器,因此发生死锁。

这种情况不一致,因为它取决于一个微妙的计时问题,取决于线程的调度方式 - 可能是 Alphonse.bowbower.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.
}
}
}

(*) 不能保证没有死锁,因为 System.identityHashCode可以为不同的对象返回相同的值;但这不太可能。

这是 Birthday paradox的应用程序:如果你只有两台显示器,碰撞的几率大约是 10^-18;但是,如果您有超过 77k 的显示器,则更有可能发生碰撞。

关于java - 使用 System.out.format 和 System.out.println 进行多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com