我发现在经典 Java Deadlock Tutorial 中包括对 System.out.format 的调用将防止发生死锁,我不明白为什么。
下面的代码与教程中的代码相同,只是在 main
中添加了 System.out.format("Hi, I'm %s...no deadlock为你!\n\n", alphonse.getName());
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) throws InterruptedException {
final Friend alphonse = new Friend("Alphonse");
final Friend gaston = new Friend("Gaston");
System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
这是输出:
Hi, I'm Alphonse...no deadlock for you!
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!
删除有问题的行会导致通常的死锁:
Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed to me!
... deadlock ...
对 System.out.format 的调用是否以某种方式改变了线程获取对象内部锁的方式?
更新:
我能够通过更改我在代码中启动线程的位置让系统再次死锁:
public static void main(String[] args) throws InterruptedException {
final Friend alphonse = new Friend("Alphonse");
final Friend gaston = new Friend("Gaston");
System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());
Thread t1 = new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
});
Thread t2 = new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
});
t1.start();
t2.start();
}
这引出了一个问题,即我们如何才能更深入地了解线程调度程序的行为方式,但我会在另一天再谈。
最佳答案
您并没有真正消除死锁,而是(由于某些内部 JVM 原因)更改了线程的计时,以便其中一个线程进入 bowBack()
在其他调用bow()
之前。
只需输入 bow
: sleep(1000)
,您的死锁就会重新出现。
请注意,死锁不会总是发生,只有当线程处于幸运时序时才会发生。在这种情况下,当两个线程都进入 bow
并且在它们中的任何一个调用 bowBack
... “一些内部 JVM 原因” 可以是以下内容:
在您的例子中,实际上有三个线程:执行main、t1 和t2 的线程。
放置 print 隐藏死锁的原因可能是线程调度程序决定 main
仍然有工作要做,即刷新 io 缓冲区,因此让 main 在开始 t1 之后和开始 t2 之前继续。如果你在双核 cpu 上,只有 main
和 t1
会运行,但 t2
会等待,因为 print
是一个缓慢的操作。上下文切换需要更多时间,t1 会在 t2 开始之前完成……因此不会发生死锁。但这并不意味着如果您再次运行该程序就不会发生死锁。
如果你想玩,创建一个队列
并在该队列中推送 token (线程名称),然后在main中加入
你的线程。完成后,打印队列内容,可以观察线程的时序。
关于java - System.out.format 如何防止死锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12450547/