我有一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 mAccounts;
private int mSlots;
public Bank(int slots) {
mAccounts = new ArrayList(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);
}
}
但是编译器警告我有关 嵌套的同步块
,我相信这是一件坏事吗?另外,我不太喜欢最大/最小解决方案(我不是那个想法的提出者),如果可能的话,我想避免这种情况。
如何解决以上两个问题?如果我们可以锁定多个对象,则可以同时锁定from和to帐户,但据我所知不能这样做。那有什么解决方案?