避免几个对象事务死锁的最佳方法是什么?

时间:2017-12-06 17:47:27

标签: java multithreading concurrency synchronized

我正在寻找死锁示例,偶然发现了这段代码:

array.OrderBy(x => Math.Abs((long)x - targetNum)).First();

您如何更改package com.example.thread.deadlock._synchronized; public class BankAccount { double balance; int id; BankAccount(int id, double balance) { this.id = id; this.balance = balance; } void withdraw(double amount) { // Wait to simulate io like database access ... try {Thread.sleep(10l);} catch (InterruptedException e) {} balance -= amount; } void deposit(double amount) { // Wait to simulate io like database access ... try {Thread.sleep(10l);} catch (InterruptedException e) {} balance += amount; } static void transfer(BankAccount from, BankAccount to, double amount) { synchronized(from) { from.withdraw(amount); synchronized(to) { to.deposit(amount); } } } public static void main(String[] args) { final BankAccount fooAccount = new BankAccount(1, 100d); final BankAccount barAccount = new BankAccount(2, 100d); new Thread() { public void run() { BankAccount.transfer(fooAccount, barAccount, 10d); } }.start(); new Thread() { public void run() { BankAccount.transfer(barAccount, fooAccount, 10d); } }.start(); } } 方法以使其不会导致死锁?首先想到的是为所有帐户创建共享锁,但这当然会破坏所有并发。那么有一种很好的方法可以锁定涉及交易的两个帐户而不影响其他帐户吗?

2 个答案:

答案 0 :(得分:4)

在多锁情况下避免死锁的一种方法是始终以相同的顺序锁定对象。

在这种情况下,这意味着您要为所有BankAccount个对象创建总排序。幸运的是我们有一个我们可以使用的id,所以你总是可以先锁定较低的id,然后再锁定(在另一个synchronized块中)较高的id。

这假设没有BankAccount个具有相同ID的对象,但这似乎是一个合理的假设。

答案 1 :(得分:-2)

分别使用两个同步块而不是嵌套。

synchronized(from){
    from.withdraw(amount);
}
synchronized(to){
    to.deposit(amount);
}

因此,在from.withdraw(amount)被调用后,from上的锁定会在尝试锁定to之前被释放