gpt4 book ai didi

Java并发实践: Listing 8. 3.引入锁顺序以防止死锁

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:27 25 4
gpt4 key购买 nike

我无法理解为什么书中 list 8.3中提到的代码不容易出现死锁。当我运行代码时,它不会导致死锁。我借鉴了书中的想法并修改了代码。以下是程序:-

    public class LockOrderingDeadLockSolved {
private final Object left = new Object();
private final Object right = new Object();
private final Object tieLock = new Object();

public static void main(String[] args) {

LockOrderingDeadLockSolved obj = new LockOrderingDeadLockSolved();
int leftHash = System.identityHashCode(obj.left);
int rightHash = System.identityHashCode(obj.right);
System.out.println(leftHash +" --- " + rightHash);
Thread t = new Thread() {
public void run() {
if (leftHash < rightHash)
obj.leftRight();
else if (leftHash > rightHash)
obj.rightLeft();
else
obj.tieLockMethod();
}
};
Thread t1 = new Thread() {
public void run() {
if (leftHash < rightHash)
obj.leftRight();
else if (leftHash > rightHash)
obj.rightLeft();
else
obj.tieLockMethod();
}
};
t.start();
t1.start();

}

private void leftRight() {
synchronized (left) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (right) {
System.out.println("Left right -- left right lock");
}
}
}

private void rightLeft() {
synchronized (right) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (left) {
System.out.println("Right left -- right left lock");
}
}
}

private void tieLockMethod() {
synchronized (tieLock) {
synchronized (left) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (right) {
System.out.println("Right left --- tie lock");
}
}
}
}
}

输出:-

865113938 --- 1442407170

左右——左右锁

左右——左右锁

容易出现死锁的程序:-

public class LockOrderingDeadLock {
private final Object left = new Object();
private final Object right = new Object();

public static void main(String[] args) {
LockOrderingDeadLock obj = new LockOrderingDeadLock();
Thread t = new Thread() {
public void run() {
obj.leftRight();
}
};
Thread t1 = new Thread() {
public void run() {
obj.rightLeft();
}
};
t.start();
t1.start();

}

private void leftRight() {
synchronized (left) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (right) {
System.out.println("Left right");
}
}
}

private void rightLeft() {
synchronized (right) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (left) {
System.out.println("Right left");
}
}
}

}

最佳答案

如果以相同的顺序获取锁,则不会发生死锁。

为了使示例死锁,您必须以不同的顺序重复获取相同的锁。这可以通过以下代码来完成。

private static final Object left = new Object();
private static final Object right = new Object();

public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
synchronized (left) {
synchronized (right) {
System.out.println("Left right -- left right lock");
}
}
}
});
Thread t2 = new Thread(() -> {
while (true) {
synchronized (right) {
synchronized (left) {
System.out.println("Right left -- right left lock");
}
}
}
});
t1.start();
t2.start();
}

关于Java并发实践: Listing 8. 3.引入锁顺序以防止死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54695652/

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