Ste*_*eve 2 java multithreading deadlock println
我在Oracle的Java Tutorial中遇到了这个例子,描述了多线程场景中的死锁.
所以在这个例子中,我在第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();
}
}
Run Code Online (Sandbox Code Playgroud)
执行这些更改后,程序成功终止,不会导致死锁并打印后输出
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!
Run Code Online (Sandbox Code Playgroud)
所以我的问题是 - 为什么它会像这样?println语句是如何防止死锁的?
你是否使用System.out.print或没有区别System.out.format:他们基本上做同样的事情.
如果Gaston.bow(Alphonse)在开始Alphonse.bow(Gaston)和之间开始执行bower.bowBack(Alphonse)(反之亦然),则发生死锁:两个线程正在等待另一个线程持有的监视器,因此发生死锁.
出现这种情况不一致,因为它依赖于一个微妙的时机的问题,这取决于线程是如何安排-这是可能的,Alphonse.bow并且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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
(*)它不能完全保证死锁,因为System.identityHashCode可以为不同的对象返回相同的值; 但这是不太可能的.
这是生日悖论的应用:如果你只有两台显示器,那么碰撞的可能性就像10 ^ -18; 但是如果你有超过77k的显示器,那么碰撞就更有可能了.
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |