我有一个Bank
类,其中包含一系列Account
。该银行有一个transfer()
方法,用于将资金从一个帐户转移到另一个帐户。想法是在转账过程中锁定from
和to
两个帐户。
为了解决这个问题,我有以下代码(请注意,这只是一个非常简单的示例):
public class Account {
private int mBalance;
public Account() {
mBalance = 0;
}
public void withdraw(int value) {
mBalance -= value;
}
public void deposit(int value) {
mBalance += value;
}
}
public class Bank {
private List<Account> mAccounts;
private int mSlots;
public Bank(int slots) {
mAccounts = new ArrayList<Account>(Collections.nCopies(slots, new Account()));
mSlots = slots;
}
public void transfer(int fromId, int toId, int value) {
synchronized(mAccounts.get(fromId, toId)) {
synchronized(mAccounts.get(toId)) {
mAccounts.get(fromId).withdraw(value);
mAccounts.get(toId).deposit(value);
}
}
}
}
这个方案可以运行,但是不能预防死锁。为了解决这个问题,我们需要将同步方式更改为以下方式:
synchronized(mAccounts.get(Math.min(fromId, toId))) {
synchronized(mAccounts.get(Math.max(fromId, toId))) {
mAccounts.get(fromId).withdraw(value);
mAccounts.get(toId).deposit(value);
}
}
但编译器警告我关于嵌套同步块,我相信那是不好的事情? 而且,我不太喜欢max/min解决方案(那不是我想出来的),如果可能的话,我想避免使用它。
如何解决上述两个问题? 如果我们可以锁定多个对象,我们将同时锁定from
和to
账户,但我们不能这样做(据我所知)。 那么解决方案是什么呢?